Mulpile file upload using node js

Hi All,

I am able to upload single image using nodejs to nuxeo server. But am not able to upload multiple images. Please let me know how to upload multiple file.

Below is my code for file upload.

uploadContent : function(request, response, next) {

    var headers = request.headers;
    var body = request.body;
    var batchId = uuid.v1();
    var customer = headers.customer; 
    var type = request.query.type;
    if(customer === undefined){
        console.log("customer is not set in header, expected to set as query parameter..");
        customer = request.query.customer;
        if(customer === undefined){
            console.log("customer is not set..");
            response.end();
            return;
        }
    }   
    if(type === undefined){
        console.log("As Content Type is not set as type query parameter, it's setting default Picture");
        type = "Picture";
    }  
    var workspace = customer + "-workspace";
    var folderPath;
    folderPath = request.query.path;        
    if(folderPath === undefined) {
        folderPath = "";
    }
    console.log("Folder Path : "+folderPath);
    var file = request.files.file, path = './uploads/';///var/cv/ams/uploads/';
    console.log("file = "+file);
    console.log("path = "+path);
    var buffer = file.buffer, // Note: buffer only populates if you set
    // inMemory: true.
    fileName = file.name;
    console.log("File name : "+fileName);
    console.log("Buffer : "+buffer);
    console.log('Batch Id : '+batchId);
    rest.post(
            nuxeo_base_url + 'automation/batch/upload',
            {
                multipart : true,
                username : cms_user,
                password : cms_password,
                headers : {
                    'X-Batch-Id' : batchId,
                    'X-File-Idx' : '0',
                    'X-File-Name' : fileName
                },
                data : {
                    'file' : rest.file(path + fileName, fileName, file.size,
                            file.encoding, file.mimetype)
                }
            }).on('complete', function(result) {
        if (result instanceof Error) {
            console.log('Error:', result.message);
            this.retry(5000); // try again after 5 sec
        } else {
            //response.setHeader("Content-Type", "application/json");
            //response.write(result);
            console.log('Uploading has been done for batch : '+batchId);
            //console.log(result);
            //response.end();
            var url = base_url + '/path' + base_workspace + '/' + workspace + "/" + folderPath;
            console.log(url);
            var data = '{"entity-type": "document", "name": "' + fileName + '", "type": "' + type + '", "properties": {"dc:title": "' + fileName + '", "dc:description": "", "file:content": {"upload-batch":"' + batchId +'", "upload-fileId":"0"}}}';
            rest.post(
                    url,
                    {
                        username : cms_user,
                        password : cms_password,
                        headers : {
                            'Content-Type' : 'application/json'
                        },
                        data : data
                    }).on('complete', function(result) {
                if (result instanceof Error) {
                    console.log('Error:', result.message);
                    this.retry(5000); // try again after 5 sec
                } else {
                    response.setHeader("Content-Type", "application/json");
                    response.write(result);
                    console.log("Uploading completed..");

                    response.end();
                }
            });
        }
    });
},
0 votes

1 answers

3284 views

ANSWER



Hi,

You need to upload each files on the same batchId but with a different X-File-Idx header (0, 1, 2, …).

You can have a look at this video explaining how to upload files and use them while creating a document: https://university.nuxeo.io/nuxeo/university/#!/course/working-with-nuxeo-platform-rest-api/importing-files-rest-api

However, why are you not using the official npm package?

0 votes