saveEmbeddedFile(filePath, embeddedId, data)
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
filePathstringrequiredAbsolute path to document where you want to save a new embedded file.
embeddedIdstringrequiredEmbedded file name. Must be at least 20 characters — use a GUID-based name so it is unique (error 9 otherwise). The document data must reference it in a field, or the file is deleted automatically.
datastring or byte[]requiredEither base64 encoded string or an array of bytes.
Returns
true when the file is saved. Throws an error otherwise, for example when the document is not found, when an embedded file with the same name already exists, when data is not base64 or a byte array, or when the document has no schema (embedded files require a schema-bound document).
Example
// ... 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/gif")
throw new Error("Only JPEG, PNG or GIF 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 embedded file in the 'Image' field
docly.patchFile("Member users/" + email, { Image: filename });
} else {
throw new Error("Profile picture upload failed");
}