Skip to content

Commit 744068f

Browse files
authored
Add file upload script and update readme (#8)
1 parent 3019b4f commit 744068f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ $A.get("e.force:refreshView").fire();
115115
compound fields. I will update this section as I do so.
116116
- To insert `ContentVersion` make sure to set `VersionData` to base64 data.
117117
Refer to the example [here](./scripts/jsButton/createContactFiles.js) for details.
118+
- The maximum size of files I was able to upload was around 2 MB.
119+
Anything larger will fail silently due to heap size limits in Apex
118120

119121
### Using Salesforce (and other) APIs in your script
120122

scripts/jsButton/fetchAndAddFile.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Go to https://cors-anywhere.herokuapp.com/ and request access to the demo for the below `fetch` statement to work */
2+
/* Also add https://cors-anywhere.herokuapp.com/ to CSP trusted sites list */
3+
let respBlob = await fetch(
4+
"https://cors-anywhere.herokuapp.com/https://www.eurofound.europa.eu/sites/default/files/ef_publication/field_ef_document/ef1663en.pdf"
5+
).then((resp) => resp.blob());
6+
7+
const blobToBase64 = (blob) => {
8+
return new Promise((resolve, reject) => {
9+
try {
10+
const reader = new FileReader();
11+
reader.readAsDataURL(blob);
12+
reader.onloadend = function () {
13+
let base64data = reader.result;
14+
debugger;
15+
resolve(base64data.substring(base64data.indexOf(",") + 1));
16+
};
17+
} catch (e) {
18+
reject(e);
19+
}
20+
});
21+
};
22+
23+
let base64data = await blobToBase64(respBlob);
24+
let cv = [
25+
{
26+
Title: "SF.pdf",
27+
VersionData: base64data,
28+
PathOnClient: "Salesforce.pdf",
29+
FirstPublishLocationId: recordId
30+
}
31+
];
32+
let fileIds = await dml.insert(cv, "ContentVersion");
33+
console.log("file ids", fileIds);
34+
toast("File added", "success");
35+
$A.get("e.force:refreshView").fire();

0 commit comments

Comments
 (0)