Last active
October 14, 2016 01:15
-
-
Save magnet88jp/c502ef48f68837c51b40c051c1b17712 to your computer and use it in GitHub Desktop.
Apex Demo for zip file download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class ZipDownloadDemoController { | |
// Constructor | |
public ZipDownloadDemoController() { | |
} | |
@RemoteAction | |
public static PageReference getZipDownloadUrl(String objectId) { | |
Zippex newZip = new Zippex(); | |
List<Attachment> attachments = [SELECT Name, Body FROM Attachment WHERE ParentId = :objectId]; | |
for(Attachment att : attachments ) { | |
newZip.addFile('folder/' + att.Name, att.Body, null); | |
} | |
Blob zipData = newZip.getZipArchive(); | |
Document d = new Document(); | |
d.name = 'demo.zip'; | |
d.body = zipData; | |
d.folderId = UserInfo.getUserId(); | |
insert d; | |
PageReference pageRef = new PageReference('/servlet/servlet.FileDownload?file=' + d.Id); | |
pageRef.setRedirect(true); | |
return pageRef; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment