I am trying to upload file to contentversion object using javascript with the help of FETCH API. I'm able to upload the file successfully but the content is not displaying. Please find the form structure below.

 const boundary = "YUTBT567TYHT";
 let data = "";
 data += `--${boundary}\r\n`;
 data += `Content-Type:application/octet-stream\r\n`;
 data += `Content-Disposition: form-data; name="VersionData";`+ 
 data += '\r\n';
 data +=  this.allFiles[0].base64 + '\r\n';
 data += `--${boundary}\r\n`;
 data += 'Content-Disposition: form-data; ' + `name="entity_content";\r\n`
 data += `Content-Type: application/json\r\n`;
 data += '\r\n';
 data += '{\r\n';
 data += '"ReasonForChange" : "Marketing materials updated",\r\n';
 data += '"PathOnClient" : "'+this.allFiles[0].filename+'"\r\n';
 data += '}\r\n';                
 data += `--${boundary}--`;
 fetch(url, { 
     method: "POST",            
     headers: {
          'Authorization': bearer,                
           'Content-Type': 'multipart/form-data; boundary='+boundary
     body : data
.then((response) => console.log(response.json()))
.then((resultdata) => {

How to uploading the file without corrupting content from Javascript ?

  • 1
    The data can't be base-64 encoded. It needs to be binary data. This also means you can't directly just make a String like this and expect it to work. You'll need to use something like FormData or build your own Request.
    – sfdcfox
    Jan 28 at 14:01
  • hi @raja-sabarish! how were you able to pass through the CORS blocking error? i already added the domain to CORS and CSP as well as added some wildcard in CORS to make it work but it doesnt. can you help me?
    – Clyde
    Sep 4 at 14:48


You must log in to answer this question.