JavaScript SDK Samples - Files Operations
Upload Files
class File
{
/**
* Upload Files
* This method is used to upload a file and print the response.
* @param {String} absoluteFilePath The absolute file path of the file to be attached
*/
static async uploadFiles() {
//example
//let absoluteFilePath = "/Users/user_name/Desktop/download.png";
//Get instance of FileOperations Class
let fileOperations = new ZCRM.File.Operations();
//Get instance of FileBodyWrapper Class that will contain the request body
let request = new ZCRM.File.Model.BodyWrapper();
let paramInstance = new ParameterMap();
await paramInstance.add(ZCRM.File.Model.UploadFilesParam.TYPE, "inline");
/** StreamWrapper can be initialized in any of the following ways */
var filesToLoad = document.getElementById("file_api").files;
var files = [];
for (var fileData of filesToLoad) {
/**
* param 1 -> fileName
* param 2 -> Read Stream.
*/
let streamWrapper = new StreamWrapper.Model.StreamWrapper(null, fileData);
files.push(streamWrapper);
}
request.setFile(files);
//Call uploadFile method that takes BodyWrapper instance as parameter.
let response = await fileOperations.uploadFiles(request, paramInstance);
if (response != null) {
//Get the status code from response
console.log("Status Code: " + response.getStatusCode());
//Get object from response
let responseObject = response.getObject();
if (responseObject != null) {
//Check if expected ActionWrapper instance is received.
if (responseObject instanceof ZCRM.File.Model.ActionWrapper) {
//Get the array of obtained action responses
let actionResponses = responseObject.getData();
actionResponses.forEach(actionResponse => {
//Check if the request is successful
if (actionResponse instanceof ZCRM.File.Model.SuccessResponse) {
//Get the Status
console.log("Status: " + actionResponse.getStatus().getValue());
//Get the Code
console.log("Code: " + actionResponse.getCode().getValue());
console.log("Details");
//Get the details map
let details = actionResponse.getDetails();
if (details != null) {
Array.from(details.keys()).forEach(key => {
console.log(key + ": " + details.get(key));
});
}
//Get the Message
console.log("Message: " + actionResponse.getMessage().getValue());
}
//Check if the request returned an exception
else if (actionResponse instanceof ZCRM.File.Model.APIException) {
//Get the Status
console.log("Status: " + actionResponse.getStatus().getValue());
//Get the Code
console.log("Code: " + actionResponse.getCode().getValue());
console.log("Details");
//Get the details map
let details = actionResponse.getDetails();
if (details != null) {
Array.from(details.keys()).forEach(key => {
console.log(key + ": " + details.get(key));
});
}
//Get the Message
console.log("Message: " + actionResponse.getMessage().getValue());
}
});
}
//Check if the request returned an exception
else if (responseObject instanceof ZCRM.File.Model.APIException) {
//Get the Status
console.log("Status: " + responseObject.getStatus().getValue());
//Get the Code
console.log("Code: " + responseObject.getCode().getValue());
console.log("Details");
//Get the details map
let details = responseObject.getDetails();
if (details != null) {
Array.from(details.keys()).forEach(key => {
console.log(key + ": " + details.get(key));
});
}
//Get the Message
console.log("Message: " + responseObject.getMessage().getValue());
}
}
}
}
}
Get File