2014-03-05 107 views
0

大家好我正在開發一個salesforce上的應用程序。 我想要讀取位於visualforce頁面中zip附件內的文件的內容,但不需要解壓zip文件。 我該如何做到這一點?有沒有辦法做到這一點?在visualforce中閱讀zip附件頁面

回答

1

大家好我已經實現了這個在這裏使用JSzip庫是我的代碼 -

在頂點頁我寫javascript函數 -

function viewContent(){ 
     var zip = null; 
     var zipFileName = null; 
     var zipFileNames = null; 
     data = "{!contentAsText}"; 
     zip = new JSZip(data, {base64:true}); 
      zipFileName = 'files.zip'; 
      zipFileNames = []; 
      for(var zipfileName in zip.files){ 
       zipFileNames.push(zipfileName); 
     if(zipfileName == 'index.html'){ 
     var file = zip.files[zipfileName]; 
     var data = file.data; 
     document.getElementById('contentdiv').innerHTML = data; 
     //var data = JSZipBase64.encode(file.data); 
     } 
     } 

在控制器 -

public String contentAsText {get;set;} 

List<Attachment> atts = [Select Id, Body from Attachment where name='files.zip' limit 1]; 
     contentAsText = EncodingUtil.base64Encode(atts[0].Body); 

此鏈接將幫助您 -

http://andyinthecloud.com/2012/12/09/handling-office-files-and-zip-files-in-apex-part-2/

2

更新修改的問題:

看一看Handling Office Files and Zip Files in Apex – Part 2由安德魯·福塞特。


有一個關於如何使用存儲在附件中的圖像進行此操作的基本知識文章。請參閱How can I Display Base64 Data on page layout?

在此示例中,AttachmentID通過查詢字符串參數傳遞,但您可以查看它,但最適合您的需求。

Visualforce頁面:

<apex:page controller="ViewImage" cache="true"> 
     <img src="data:{!att.ContentType};base64,{!image}" /> 
</apex:page> 

控制器:

public class ViewImage { 
     public Attachment att { 
      get { 
        if (att == null) { 
         String id = ApexPages.currentPage().getParameters().get('AttachmentID'); 
         att = [SELECT Body, ContentType, Name FROM Attachment WHERE ID = :id]; 
        } 
        return att; 
      } 
      private set; 
     } 
     public String image { 
      get { 
        return EncodingUtil.Base64Encode(att.body); 
      } 
     } 
} 
+0

感謝您的評論丹尼爾。但我有附件作爲壓縮文件,我想讀取該壓縮文件內的文件,而無需解壓該zip附件。 –