Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active February 2, 2020 08:59
Show Gist options
  • Save dhaniksahni/11baffcbbe1d6cd1bca4b716444cebe8 to your computer and use it in GitHub Desktop.
Save dhaniksahni/11baffcbbe1d6cd1bca4b716444cebe8 to your computer and use it in GitHub Desktop.
File Reader from File object in Salesforce
<aura:component
controller="FileReader"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"
access="global">
<aura:attribute name="data" type="String" />
<aura:attribute name="previewUrl" type="String" />
<aura:attribute name="urlLink" type="String" />
<aura:attribute name="recordId" type="String" />
<aura:attribute name="files" type="object[]" />
<aura:attribute name="file" type="object" />
<aura:attribute name="showFileLink" type="boolean" />
<lightning:button variant="brand" label="View Files" onclick="{!c.getSpecificFile}" /><br/>
<aura:if isTrue="{!v.showFileLink}">
<a href="{!'data:' + v.file.ContentType+';base64,'+v.file.Content}">Download File</a><br/>
<a href="{!v.file.FileUrl}">Preview File</a><br/>
<a href="{!v.file.DownloadUrl}">Download using Link</a>
</aura:if>
<lightning:button variant="brand" label="View Case Files" onclick="{!c.getCaseFiles}" /><br/>
<aura:iteration items="{!v.files}" var="f">
<a href="{!'data:' + f.ContentType+';base64,'+f.Content}">Download File</a><br/>
<a href="{!f.FileUrl}">Preview File</a><br/>
<a href="{!f.DownloadUrl}">Download using Link</a><br/>
</aura:iteration>
</aura:component>
({
getSpecificFile : function(component, event, helper) {
var recordId=component.get("v.contentdocumentId");
//Remove hardcoding based on your record
helper.requestFiledata(component, event,'0692v00000D1pBIAAZ','');
},
getCaseFiles : function(component, event, helper) {
var recordId=component.get("v.recordId");
//Remove hardcoding based on your record
helper.requestFiledata(component, event, '5002v00002g58RPAAY','entity');
}
})
({
requestFiledata: function(component, event,recordid,type) {
//**dataURL to blob**
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
//**blob to dataURL**
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {
callback(e.target.result);
};
a.readAsDataURL(blob);
}
var action = type=='entity'?component.get("c.GetEntityRecordFiles"):component.get("c.GetFile");
action.setParams({
recordId: recordid
});
action.setCallback(this, function(a) {
var state = a.getState();
if (state == "SUCCESS") {
var res = a.getReturnValue();
component.set(type=='entity'?"v.files":"v.file", res);
}
});
$A.enqueueAction(action);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment