Created
March 28, 2020 14:37
-
-
Save sudikrt/18ccc30dab9561347db536339be35340 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
global class DemoFileUploadCntl { | |
@RemoteAction | |
global static String createContentVersion(String data, String fileName, String parentId) { | |
ContentVersion versionData = new ContentVersion (); | |
versionData.ContentLocation = 's'; | |
versionData.VersionData = EncodingUtil.base64Decode(data); | |
versionData.PathOnClient = fileName; | |
versionData.Title = fileName; | |
insert versionData; | |
ContentDocumentLink conDocLink = new ContentDocumentLink(); | |
conDocLink.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :versionData.Id].ContentDocumentId; | |
conDocLink.LinkedEntityId = parentId; | |
conDocLink.ShareType = 'V'; | |
insert conDocLink; | |
return Json.serialize(new ContentWrapper (versionData.Id, conDocLink, versionData.Title)); | |
} | |
public class ContentWrapper { | |
public String name; | |
public Id contentId; | |
public ContentDocumentLink contentDocumentLink; | |
public ContentWrapper () { | |
name = ''; | |
} | |
public ContentWrapper (Id contentId, ContentDocumentLink contentDocumentLink, String name) { | |
this.contentId = contentId; | |
this.contentDocumentLink = contentDocumentLink; | |
this.name = name; | |
} | |
} | |
} |
This file contains hidden or 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
<apex:page controller="DemoFileUploadCntl" sidebar="false" showHeader="false" applyHtmlTag="false" applyBodyTag="false"> | |
<html> | |
<head> | |
<title>File Upload using content version</title> | |
<apex:includeScript value="{!$Resource.jQuery_3_1_1}"/> | |
<script> | |
$(function(){ | |
let totalFiles = 0; | |
let totalFilesUplodCalled = 0; | |
let totalFilesUplodSuccess = 0; | |
let totalFilesUplodFailed = 0; | |
let parentId = '0017F00002D9UBZ'; // replace this with your parentId | |
$('#uploadBtn').click(function() { | |
var v=document.getElementById('fileInput'); | |
totalFiles = v.files.length; | |
document.getElementById ('totalFiles').innerHTML = totalFiles; | |
for (var i = 0; i < v.files.length; i++) { | |
filetoBase64( v.files[i], function(err, content, file){ | |
Visualforce.remoting.Manager.invokeAction( | |
'{!$RemoteAction.DemoFileUploadCntl.createContentVersion}', | |
content, | |
file.name, | |
parentId, | |
function(result, event){ | |
onFileUploadFinished (result, event); | |
if (event.status) { | |
} else if (event.type === 'exception') { | |
} else { | |
} | |
}, | |
{escape: false} | |
); | |
}); | |
} | |
}); | |
var onFileUploadFinished = function (result, event) { | |
totalFilesUplodCalled++; | |
console.log ('result :' + result); | |
console.log ('event :' + event); | |
if (event.status) { | |
totalFilesUplodSuccess ++; | |
} else { | |
totalFilesUplodFailed ++; | |
} | |
if (totalFilesUplodCalled == totalFiles) { | |
console.log ('All file upload process is called'); | |
//stop progress; | |
} | |
document.getElementById ('totalFilesUplodCalled').innerHTML = totalFilesUplodCalled; | |
document.getElementById ('totalFilesUplodSuccess').innerHTML = totalFilesUplodSuccess; | |
document.getElementById ('totalFilesUplodFailed').innerHTML = totalFilesUplodFailed; | |
} | |
var filetoBase64 = function(file, callback){ | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
var myFileContents = reader.result; | |
var base64Mark = 'base64,'; | |
var dataStart = myFileContents.indexOf(base64Mark) + base64Mark.length; | |
myFileContents = myFileContents.substring(dataStart); | |
callback(null, myFileContents, file); | |
} | |
reader.readAsDataURL(file); | |
} | |
}); | |
</script> | |
<style> | |
.slds-scope .slds-page-header{ | |
border-radius: 0px; | |
box-shadow: 0 0px 0px 0 rgba(0, 0, 0, 0.1); | |
} | |
.bodyPart{ | |
padding:10px; | |
} | |
</style> | |
</head> | |
<apex:slds /> | |
<body class="slds-scope"> | |
<div class="slds-m-around_small" style="border:1px solid #D9D9D9;"> | |
<div class="slds-page-header"> | |
<span style=""> Upload Files</span> | |
</div> | |
<div> | |
<div class="slds-p-around_small"> | |
<div> | |
totalFiles : <span id="totalFiles"></span> | |
</div> | |
<div> | |
totalFilesUplodCalled : <span id="totalFilesUplodCalled"></span> | |
</div> | |
<div> | |
totalFilesUplodSuccess : <span id="totalFilesUplodSuccess"></span> | |
</div> | |
<div> | |
totalFilesUplodFailed : <span id="totalFilesUplodFailed"></span> | |
</div> | |
</div> | |
</div> | |
<div class="slds-p-around_small"> | |
<pre> | |
Setup: | |
1. Allow guset users to upload files | |
2. Create site, Allow access to this vf page | |
3. Update the parent Id to which you would wanted to attached. | |
Note: This is Just a POC, You have to take care of error handling, Like nulls of parent Id, | |
what if file upload failed? etc.. :( And its free :) | |
</pre> | |
</div> | |
<div class="bodyPart"> | |
Select file: <input type="file" name="files" multiple="multiple" id="fileInput"/> | |
<input type="button" id="uploadBtn" name="Address" value="Upload" class="slds-button slds-button--brand"/> | |
</div> | |
</div> | |
</body> | |
</html> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment