0
votes

Hi all I am developing an app on salesforce. I want to read the content of the file which is inside the zip attachment in visualforce page but without extracting the zip file. How can I achieve this? Is there any way to do this?

2

2 Answers

2
votes

Update for modified question:

Have a look at Handling Office Files and Zip Files in Apex – Part 2 by Andrew Fawcett.


There is a basic Knowledge Article on how to do this with an image that is stored in an Attachment. See How can I Display Base64 Data on page layout?

In this example the AttachmentID is passed via a query string paramemter, but you could look it up however works best for your requirement.

Visualforce page:

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

Controller:

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);
            }
      }
}
1
votes

Hi all I have achieved this using JSzip library here is my code --

In apex page I have written javascript function --

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);
         }
         }

In controller --

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);

This link will help you --

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