function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vivek gvivek g 

Unable to view .Xls files after uploading in Documents Standard Object.

Hi EveryOne,

As of my Requirement Craeted VF page and Apex class to Upload a Document in to Documents Object.

its working fine for all (Upload & Read).

But facing Issue after uplaoding .Xls files to read(after clicking on view link).

VF Code:
===============================
<apex:inputFile value="{!document.body}" filename="{!document.name}" contentType="{!document.contentType}" id="file" accept="{!document.contentType}"/>

Thanks,
Vivek G.
Gaurav KheterpalGaurav Kheterpal
Can you share your complete code? I suspect it may be a problem that the uploaded file is being corrupted because you are not setting multi-part boundaries correctly.

If my answer helps resolve your query, please mark it as the 'Best Answer' to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Developer Forums Moderator| Dreamforce Speaker

    




 
vivek gvivek g
@Gaurav Kheterpal,

Thanks for your repy..

VF Code:

<!--For uploading the document-->
<apex:page controller="Repository_apex">

 <apex:sectionHeader title="Add Documents"/>
 
  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />    
    <apex:actionStatus id="stat" style="align:center;">
          <apex:facet name="start"> 
              <apex:outputPanel styleClass="customPopup" layout="block" >
                   <div ><apex:image style="width:10%;allign:center;" value="{!$Resource.Loading}"/></div>
              </apex:outputPanel>                
          </apex:facet> 
          <apex:facet name="stop"/>
    </apex:actionStatus>
    
    <apex:pageBlock title="Upload a File">
 
      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
        <apex:commandButton action="{!Documents}" value="Documents"/>
      </apex:pageBlockButtons>
      
      &nbsp;&nbsp;&nbsp;<b>Folder</b>&nbsp;
          <apex:actionRegion >
          <apex:selectList size="1" value="{!FolderName}" id="folder" >
              <apex:actionSupport event="onchange" action="{!GetDocuments}" rerender="Relateddocs" status="stat"/>
              <apex:selectoption itemvalue="Please Select"/>
              <apex:selectoption itemvalue="Legal"/> 
              <apex:selectoption itemvalue="Credit"/>
              <apex:selectoption itemvalue="Operation"/>
              <apex:selectoption itemvalue="HFC"/>
          </apex:selectList>
          </apex:actionRegion>
      
      <apex:pageBlockSection showHeader="false" columns="2" id="block1">    
          <apex:pageBlockSectionItem >              
              <apex:outputText />
          </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem >
              <apex:outputText />
          </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem >
          <apex:outputLabel value="Document Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>
 
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!document.body}" filename="{!document.name}" contentType="{!document.contentType}" id="file" accept="{!document.contentType}"/>
        </apex:pageBlockSectionItem>
 
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!document.description}" id="description"/>
        </apex:pageBlockSectionItem>
 
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Keywords" for="keywords"/>
          <apex:inputText value="{!document.keywords}" id="keywords"/>
        </apex:pageBlockSectionItem>
 
      </apex:pageBlockSection>
    </apex:pageBlock>
    
    <apex:pageBlock title="Related Documents">  
      <apex:pageblockTable value="{!RelatedDocuments}" var="RD" id="Relateddocs">
              <apex:column value="{!RD.name}" headerValue="Document Name"/>  
              <apex:column value="{!RD.description}" headerValue="Description"/>            
              <apex:column headerValue="File">                   
                   <apex:outputLink value="https://c.cs5.content.force.com/servlet/servlet.FileDownload?file={!RD.id}" target="_blank">
                    View</apex:outputLink>
               </apex:column>               
          </apex:pageblockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Code:
===============================================================================

//For uploading the document in repository
public class Repository_apex{
 
  public string FolderName{get;set;}
  public list<Document> RelatedDocuments{get;set;}
  //public string Docid{get;set;}
  
  public Document document {
    get {
      if (document == null)
        document = new Document();
      return document;
    }
    set;
  }
  
  //this method will call when user clicks on Save button 
  public PageReference upload() {
    
    if(FolderName != 'Please Select' && document.name!='' && document.Body!=NULL){
        //document.AuthorId = UserInfo.getUserId();
                
        Folder Fold = [Select Id from Folder where Name=:FolderName limit 1];
        
        document.FolderId = Fold.id;
        
        try {
              insert document;
            } catch (DMLException e) {
              ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
              return null;
            } finally {
              document.body = null; // clears the viewstate
              document.description = null;
              document.keywords = null;
              document = new Document();
              
              RelatedDocuments = [select id,name,description from Document where Folder.name=:FolderName order by Name];
            }
         
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));        
        }        
                
      }else
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Folder, Document Name & File fields are mandatory'));
           
       return null;
    }
    
    //this method will call when user clicks on Documents button 
    public PageReference Documents() {
        
        PageReference ProdPage = new PageReference('/015/o');        
        
        return ProdPage;
        
    } 
    
    //this method will call when user changes Folder Value
    public void GetDocuments(){
        RelatedDocuments = [select id,name,description from Document where Folder.name=:FolderName order by Name];
    }    
    
}