Node JS SDK Samples - Files Operations
Upload Files
const fs = require("fs");
const path = require("path");
const {FileOperations, GetFileParam, UploadFilesParam} = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/file_operations");
const FileBodyWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/file_body_wrapper").FileBodyWrapper;
const BodyWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/body_wrapper").BodyWrapper;
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/success_response").SuccessResponse;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/api_exception").APIException;
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/file/action_wrapper").ActionWrapper;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class File{
/**
* Upload File
* 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 FileOperations();
//Get instance of FileBodyWrapper Class that will contain the request body
let request = new BodyWrapper();
let paramInstance = new ParameterMap();
await paramInstance.add(UploadFilesParam.TYPE, "inline");
/** StreamWrapper can be initialized in any of the following ways */
/**
* param 1 -> fileName
* param 2 -> Read Stream.
*/
// let streamWrapper1 = new StreamWrapper(null, fs.createReadStream("/Users/user_name/Desktop/file1.txt"));
// let streamWrapper3 = new StreamWrapper(null, fs.createReadStream("/Users/user_name/Desktop/file2.txt"));
/**
* param 1 -> fileName
* param 2 -> Read Stream
* param 3 -> Absolute File Path of the file to be attached
*/
let streamWrapper2 = new StreamWrapper(null, null, "/Users/user_name/Desktop/file3.txt");
//Set file to the FileBodyWrapper instance
// request.setFile([streamWrapper1, streamWrapper2, streamWrapper3]);
request.setFile([streamWrapper2]);
//Call uploadFile method that takes BodyWrapper instance and ParameterMap instance as parameter.
let response = await fileOperations.uploadFiles(request, paramInstance);
if(response != null){
//Get the status code from response
console.log("Status Code: " + response.statusCode);
//Get object from response
let responseObject = response.object;
if(responseObject != null){
//Check if expected ActionWrapper instance is received.
if(responseObject instanceof ActionWrapper){
//Get the array of obtained action responses
let actionResponses = responseObject.getData();
actionResponses.forEach(actionResponse => {
//Check if the request is successful
if(actionResponse instanceof 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 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 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());
}
}
}
}
}
module.exports = {File}
Get File