• Mahi Dodani 8
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hello Team,

I am able to successfully upload an attachement/document as PDF on salesforce. However whenever I try to view the file, i get an error message "Failed to load file." I have followed the intsructions on the link : https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

I tried with the code as well. 

Both methods did not help

Here is the code

@RestResource(urlMapping='/UploadFile/*')
global with sharing class NG_UploadFile {

    @HttpPost
   global static String uploadAttachment(){
       RestRequest request = RestContext.request;  
           RestResponse response =  RestContext.response;
        Attachment attachmentFromReq = (Attachment)Json.deserialize(request.requestBody.toString(),Attachment.class);    
       attachmentFromReq.ContentType = 'application/pdf';       
       Blob attachmentBody = EncodingUtil.base64Decode('dGVzdCBhdHRhY2htZW50IGRhdGE=');       
       attachmentFromReq.IsPrivate = false;
       attachmentFromReq.Body = attachmentBody;
       System.debug('attachmentFromReq : ' + attachmentFromReq);
       insert attachmentFromReq;
       return attachmentFromReq.id;
   }
}

JSON Request

{
     "Name": "TestFile.pdf",
    "Description": "Test text upload",
    "ParentId" : "0030S000004rYjv"
}