Attachment Recommendations
Copy an attachment
To copy attachments, use the server-side scripts. You can use one of the following ways:
-
Use the base64 object of the original attachment. Due to the specifics of the readBase64 method, the maximum size of the copied file is 10 Mb.
Example
const attachmentRec = new SimpleRecord('sys_attachment');
attachmentRec.get('167654133500015741');
ss.info(`Original size bytes: ${attachmentRec.size_bytes}`);
// Info: Original size bytes: 92414492
const attach = new SimpleAttachment();
const base64Value = attach.readBase64(attachmentRec.sys_id);
const attachCopyId = attach.writeBase64(
attachmentRec.record_document_id,
'copy_of_' + attachmentRec.file_name,
base64Value,
attachmentRec.mime_content_type
);
if (attachCopyId) {
attachmentRec.get(attachCopyId);
ss.info(`/record/sys_attachment/${attachCopyId}`);
// Info: /record/sys_attachment/167654428100399185
ss.info(`Copy size bytes: ${attachmentRec.size_bytes}`);
// Info: Copy size bytes: 0
} -
Use the SimpleAttachmentService class. The maximum size of the copied file depends on the instance-wide limit that is specified in the max_file_size_upload system property.
Example
const attachmentRec = new SimpleRecord('sys_attachment');
attachmentRec.get('167654133500015741');
ss.info(`Original size bytes: ${attachmentRec.size_bytes}`);
// Info: Original size bytes: 92414492
const importSource = new SimpleRecord('sys_import_source');
importSource.get('167647710807446665'); // From Incident Import Source
const docId = ss.getDocIdByIds(importSource.sys_db_table_id, importSource.sys_id);
const attach = new SimpleAttachment();
const attachUrl = attach.getAttachmentUrlById(attachmentRec.sys_id);
const simpleAttach = new SimpleAttachmentService();
const docID = ss.getDocIdByIds(importSource.sys_db_table_id, importSource.sys_id);
const attachCopyId = simpleAttach.createAttachmentByUrl(attachUrl, docId, `Copy_of_the_${attachmentRec.file_name}`);
if (attachCopyId) {
attachmentRec.get(attachCopyId);
ss.info(`/record/sys_attachment/${attachCopyId}`);
// Info: /record/sys_attachment/167654472004676912
ss.info(`Copy size bytes: ${attachmentRec.size_bytes}`);
// Info: Copy size bytes: 92414492
}
Attachment download URL
Attachment links can be used for various purposes:
- downloading a single file from attachments
- downloading all files from attachments as an archive
- forming a link to an attachment file to send to the src attribute of an HTML tag
- forming a link to an attachment file to insert into an HTML field
To form a link for downloading a single file from attachments, use the following format: {your-instance-url}/v1/attachments/download/{sys_attachment.sys_id}?access-token={user_token}, where:
-
your-instance-urlis the URL of the instance where the attachment is stored. If the attachment is stored on the instance where the script is executed, use a relative URL:/v1/attachments/download/${sys_attachment.sys_id}?access-token=${s_user.accessToken}. -
sys_attachment.sys_idis the attachment record ID. -
user_tokenis the user's token.noteStarting from Platform version 1.29.0, the user's token is stored in a cookie with the HttpOnly and Secure parameters, and sending the token with the attachment download link is not required. To get the current user's token in a client script on Platform versions prior to 1.29.0, use the s_user.accessToken property.
To form a download link for multiple attachment files as an archive, use the following format: {your-instance-url}/v1/attachments/download-all/?access-token={user_token}&attachment_ids={sys_attachment.sys_id,sys_attachment.sys_id}.
The format of the link for HTML content depends on the attachment record parameters. Attachment records can be public or private. The visibility of attachments affects whether an attachment link will have an expiration date.
- Links to public attachments do not expire.
- Links to private attachments have a lifetime of 1 hour by default from the moment they are generated. The lifetime of a link is determined through the system property s3.presigned.link.lifetime.
The Public attribute on the Attachment form defines the visibility of attachments.
To generate an attachment download link via the API, use the getAttachmentUrlById() method.
Depending on the publicity of an attachment, two links can be generated. For example:
- for non-public attachments:
https://s3-{your-instance-url}/attachment/9/b7/prp5bikeyg70mpn7mhfszi4amgzleo?response-content-disposition=inline%3B%20filename%3D%22image.png... - for public attachments:
https://s3-{your-instance-url}/public-attachment/9/b7/prp5bikeyg70mpn7mhfszi4amgzleo?response-content-disposition=inline%3B%20filename%3D%22image.png...
When a non-public attachment is downloaded via a direct link, the system checks the read access to the attachment record and, if the attachment is bound to a record, the read access to that record. The check does not apply to files of Image type fields. To disable the access check, set the platform.attachments.acl.enabled property value to false. The file existence is checked even if the access check is disabled.