Skip to content

Instantly share code, notes, and snippets.

@magnet88jp
Created October 20, 2016 09:42
Show Gist options
  • Save magnet88jp/701a009b0b4bdba0335c6a481d977908 to your computer and use it in GitHub Desktop.
Save magnet88jp/701a009b0b4bdba0335c6a481d977908 to your computer and use it in GitHub Desktop.
<apex:page controller="ZipDownloadDemoController" standardStylesheets="false" showHeader="false" applyHtmlTag="false" applyBodyTag="false" >
<link rel="stylesheet" href="{! URLFOR($Resource.bootstrapsf1, 'src/dist/css/bootstrap.css')}" />
<script src="//code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Begin Default Content REMOVE THIS -->
<h1>Zip Download Demo</h1>
<div id="dldemo" class="btn btn-default">download</div>
<script>
var RemoteActionModule = ZipDownloadDemoController;
$(function() {
$('#dldemo').click(function(){
var objectId = '00T2800000qbclH';
RemoteActionModule.getZipDownloadUrl(objectId, function(result, event) {
if(event.status) {
location.href = result;
return false;
} else {
console.log('error=' + JSON.stringify(event));
}
}, {
escape: false
});
});
});
</script>
</apex:page>
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();
ContentVersion cv = new ContentVersion();
cv.Title = 'demo';
cv.PathOnClient = 'demo.zip';
cv.VersionData = zipData;
cv.IsMajorVersion = true;
cv.FirstPublishLocationId = UserInfo.getUserId();
insert cv;
PageReference pageRef = new PageReference('https://craft-ksdemo01-dev-ed--c.ap2.content.force.com/sfc/servlet.shepherd/version/download/' + cv.Id + '?asPdf=false&operationContext=CHATTER');
pageRef.setRedirect(true);
return pageRef;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment