Node JS SDK Samples - Files Operations
Upload Files
Get File
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{
/**
* Get File
* This method is used to download the file with ID and write in the destinationFolder
* @param {String} id The ID of the uploaded File.
* @param {String} destinationFolder The absolute path of the destination folder to store the File
*/
static async getFile(id, destinationFolder){
//example
//let id = "ae9c7cefa418aec1d6a5cc2d9ab35c3231aae3bfeef7d5e00a54b7563c0dd42b";
//let destinationFolder = "/Users/user_name/Desktop"
//Get instance of FileOperations Class
let fileOperations = new FileOperations();
//Get instance of ParameterMap Class
let paramInstance = new ParameterMap();
//Add the id to ParameterMap instance
await paramInstance.add(GetFileParam.ID, id);
//Call getFile method that takes ParameterMap instance as parameter
let response = await fileOperations.getFile(paramInstance);
if(response != null){
//Get the status code from response
console.log("Status Code: " + response.statusCode);
if([204, 304].includes(response.statusCode)){
console.log(response.statusCode == 204? "No Content" : "Not Modified");
return;
}
//Get object from response
let responseObject = response.object;
if(responseObject != null){
//Check if expected FileBodyWrapper instance is received
if(responseObject instanceof FileBodyWrapper){
//Get StreamWrapper instance from the returned FileBodyWrapper instance
let streamWrapper = responseObject.getFile();
//Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
let fileName = path.join(destinationFolder, streamWrapper.Name);
//Get the stream from StreamWrapper instance
let readStream = streamWrapper.Stream;
//Write the stream to the destination file.
fs.writeFileSync(fileName, readStream);
}
//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}