Skip to product menu
Skip to main content

JavaScript SDK Samples - Files Operations

Upload Files
Get File
              
              
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.
     */
    static async getFile(id) {
        //example
        //let id = "ae9c7cefd5e00a54b7563c0dd42b";

        //Get instance of FileOperations Class
        let fileOperations = new ZCRM.File.Operations();

        //Get instance of ParameterMap Class
        let paramInstance = new ParameterMap();

        //Add the id to ParameterMap instance
        await paramInstance.add(ZCRM.File.Model.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.getStatusCode());

            if ([204, 304].includes(response.getStatusCode())) {
                console.log(response.getStatusCode() == 204 ? "No Content" : "Not Modified");

                return;
            }

            //Get object from response
            let responseObject = response.getObject();

            if (responseObject != null) {
                //Check if expected FileBodyWrapper instance is received
                if (responseObject instanceof ZCRM.File.Model.FileBodyWrapper) {
                    //Get StreamWrapper instance from the returned FileBodyWrapper instance
                    let streamWrapper = responseObject.getFile();

                    //Get name from StreamWrapper instance
                    let fileName = streamWrapper.getName();

                    //Get the stream from StreamWrapper instance
                    let readStream = streamWrapper.getStream();

                    var url = URL.createObjectURL(readStream);

                    var ttt = document.createElement('a');

                    ttt.href = url;

                    ttt.download = fileName;

                    ttt.click();
                }
                //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());
                }
            }
        }
    }
}