SaveEmbeddedFile(filePath, filename, data) Last updated: 16. May 2024
API only function
Saves an embedded file to an existing Docly document. Note that embedded files that are not referenced by a field in the document will be automatically deleted.
Parameters
Name | Type | Description |
---|---|---|
filePath | string | Absolute path to document where you want to save a new embedded file. |
filename | string | Embedded filename to upload. Must be referenced by the document data and schema. A GUID based name, ensuring an unique filename should always be used. |
data | string or byte[] | Either base64 encoded string or an array of bytes. |
Returns
Boolean, true if successful otherwise throws an error.
If an embedded file with the same name alread exists for this document, this function throws an error (as GUID names should be used).
Example
Code example (JS)
JS is normal JavaScript either running in the browser or on the Docly™ server.// ... JS setting "email" variable of user to update ...
let uploads = docly.getUploads();
if (uploads.length != 1) throw new Error("Expected 1 upload");
let upload = uploads[0];
let filedata = docly.GetUploadBase64(upload.Name);
let type = upload.ContentType.toLowerCase();
if (type != "image/jpeg" && type != "image/png" && type != "image/png")
throw new Error("Only JPEG or PNG image files are supported as profile picture.");
let extension = type.split("/")[1];
let filename = docly.guid() + "." + extension;
let save = docly.SaveEmbeddedFile("Member users/" + email, filename, filedata);
if (save) {
// Reference the uploaded file in the 'Image' field
profile.Image = filename;
docly.save("Member users/" + email, profile);
} else {
throw new Error("Profilce picture upload failed");
}