SHARE
TWEET

Untitled

a guest Jun 20th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public without sharing class FileUploadController
  2.      
  3. @AuraEnabled
  4. public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
  5.  
  6.     try {
  7.  
  8.         base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
  9.  
  10.         ContentVersion contentVersion = new ContentVersion(
  11.             versionData = EncodingUtil.base64Decode(base64Data),
  12.             title = fileName,
  13.             pathOnClient =  '/' + fileName,
  14.             FirstPublishLocationId = parentId);
  15.  
  16.         insert contentVersion;
  17.  
  18.         return contentVersion.Id;
  19.  
  20.     } catch (Exception error) {
  21.         throw new AuraHandledException(error.getMessage());
  22.     }
  23. }
  24.      
  25. private static void appendToFile(Id fileId, String base64Data) {
  26.  
  27.     base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
  28.  
  29.     ContentVersion contentVersion = [
  30.         SELECT Id, versionData
  31.         FROM ContentVersion
  32.         WHERE Id = :fileId
  33.     ];
  34.  
  35.     String existingBody = EncodingUtil.base64Encode(contentVersion.versionData);
  36.  
  37.     contentVersion.versionData = EncodingUtil.base64Decode(existingBody + base64Data);
  38.  
  39.     update contentVersion;  
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top