Salesforce Asked by Vladimir K. on December 21, 2020
I am developing a visualforce page. I’m trying to upload pdf-file from remote server in contentversion , here is part of my code:
let response = await fetch('http://www.africau.edu/images/default/sample.pdf');
let blob = await response.blob();
let filename = 'File.pdf';
let _file = new File([blob], filename);
let cv = new sforce.SObject('ContentVersion');
cv.Title = filename;
cv.PathOnClient = filename;
cv.VersionData = blob;
cv.VersionData = cv.VersionData.substring(dataStart);
cv.ContentLocation = 'S';
let result = await sforce.connection.create([cv]);
console.log('result > ', result);
But file in ContentVersion is wrong.
Any ideas?
Upd.: an interesting thing: if I’ll set js debugs and try to download file locally (not to sf) – then I can do it. But only with debug points
You have to make sure that the VersionData has base64 encoded string for the file.
Add one more function to convertBlobtoBase64
export const convertBlobToBase64 = blob => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
Then modify your code to
let response = await
fetch('http://www.africau.edu/images/default/sample.pdf');
let blob = await response.blob()
.then(convertBlobToBase64)
.then((result) => {
fileContent = result;
const base64Constant = 'base64,';
const base64ImageValue =
fileContent.indexOf(base64Constant) + base64Constant.length;
this.base64Data = fileContent.substring(base64ImageValue);
let cv = new sforce.SObject('ContentVersion');
cv.Title = filename;
cv.PathOnClient = filename;
cv.VersionData = blob;
cv.VersionData = cv.VersionData.substring(dataStart);
cv.ContentLocation = 'S';
let result = await sforce.connection.create([cv]);
console.log('result > ', result);
});
You can then use the Base64 encoded string in versionData
Answered by Mohith Shrivastava on December 21, 2020
3 Asked on January 20, 2021 by user2610036
2 Asked on January 20, 2021 by isha
1 Asked on January 20, 2021 by jess-merino
2 Asked on January 19, 2021 by lauren
2 Asked on January 18, 2021 by user5105
1 Asked on January 18, 2021 by b-elliott
0 Asked on January 17, 2021 by dibyaranjan-sahoo
1 Asked on January 17, 2021
1 Asked on January 17, 2021
1 Asked on January 14, 2021 by siddharth-kumar
1 Asked on January 14, 2021 by tyler-zika
1 Asked on January 13, 2021
0 Asked on January 12, 2021 by himanshu-kumar
community lightning web components salesforcedx salesforcedx cli
3 Asked on January 12, 2021 by user65190
2 Asked on January 11, 2021 by mahwish
1 Asked on January 10, 2021 by uwe-heim
duplicate management lightning lightning aura components lightning experience
3 Asked on January 10, 2021 by andres-bravo
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP