Attaches a file to a specific document in the system.

This function performs an asynchronous request to the server to attach a file to a document. The file is specified by the filename and fileData parameters, and it is attached to the document identified by doctype and docname. Additional parameters like folder, base64_decode, is_private, and docfield can be used to control the attachment behavior and file handling.

// Attach a file to an 'Employee' document.
const fileParams: attachFileParams = {
filename: 'resume.pdf',
fileData: 'base64-encoded-data-here',
doctype: 'Employee',
docname: 'EMP-001',
folder: 'Attachments',
base64_decode: true,
is_private: true,
};
const attachedFile = await attachFile(fileParams);
console.log(attachedFile); // Logs the details of the attached file
// Attach a file to a 'Project' document without specifying a folder.
const fileParams: attachFileParams = {
filename: 'project_plan.xlsx',
fileData: 'base64-encoded-data-here',
doctype: 'Project',
docname: 'PRJ-2024',
};
const attachedFile = await attachFile(fileParams);
console.log(attachedFile); // Logs the details of the attached file

Throws an error if the request fails or if there is an issue with attaching the file.

  • Parameters

    • params: attachFileParams

      The parameters for attaching the file. This includes:

      • filename: The name of the file to be attached.
      • fileData: The data of the file, which can be in base64-encoded format.
      • doctype: The type of document to which the file will be attached.
      • docname: The name or identifier of the document to which the file will be attached.
      • folder (optional): The folder where the file will be stored. If not specified, the file is attached to the default location.
      • base64_decode (optional): Whether to decode the fileData from base64 format. Defaults to false.
      • is_private (optional): Whether the file should be marked as private. Defaults to true.
      • docfield (optional): The specific field in the document where the file should be attached. If not specified, the file is attached generally to the document.

    Returns Promise<File>

    A promise that resolves to a File object representing the attached file. The File object includes details about the file, such as its name, URL, and metadata.