0

I have created a file upload API using multer and express which works fine through POSTMAN but when i try to call the same api using another file upload API, it's not working:

My code is as follows which runs on http://localhost:7022/nuxeo/upload:

module.exports.upload = function (req, res, next) {
    var path = req.body.path
    var uploadFile = req.file; //get uploaded file
    var stream = streamifier.createReadStream(req.file.buffer) //create a stream from file buffer
    var blob = new Nuxeo.Blob({   //create a blob from file stream
        content: stream,
        name: uploadFile.originalname,
        mimeType: uploadFile.mimetype,
        size: uploadFile.size
    });
    var batch = nuxeo.batchUpload();

In the above code when I call the API through postman, my req.file is populated.

But calling the above API using the code below doesn't populate the req.file of the first API, it is undefined. I have also tried using form-data npm module without any luck :

module.exports.attach = function(req,res,next){
    var uploadfile = req.file  //file is populated here
    formData = { 'file' : uploadfile, 'path' : '/FCA/DT/clause32a'}
    var opts = {
        url: "http://localhost:7022/nuxeo/upload",
        headers: { 'enctype':  'multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
        json: true,
        body: formData
    };
    request.post(opts,function(err,response,body){
        console.log(body)
    })
}
  • If it works with postman, server side should work fine. Can you post the client side code or html – Janith Sep 05 '18 at 05:05
  • The second API acts like a client, i receive the uploaded file in the req.file of the second API which is supposed to send this req.file to the first API – Ehtesham Shareef Sep 05 '18 at 05:18
  • I'm trying to do something like this : https://stackoverflow.com/questions/46975590/how-to-upload-file-saved-in-memory-by-multer-to-another-api?rq=1 – Ehtesham Shareef Sep 05 '18 at 05:40

2 Answers2

0
module.exports.attach = function(req,res,next){
    var uploadfile = req.file  //file is populated here
    var fs = require('fs');
var request = require('request');
request.post({
    url: <URL>,
    formData: {
        file: fs.createReadStream(<FILE_PATH>),
        filetype: <FILE_TYPE>,
        filename: <FILE_NAME>,
        title: <FILE_TITLE>,
    },
}, function(error, response, body) {
    console.log(body);
});

}
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28
  • Can i just put req.file in the file property of formData ? I'm saving the file in memory through multer and it can only be accessed via req.file. On doing fs.createReadStream(req.file) I'm getting a TypeError : path must be a string or buffer. Also, req.file has a buffer property, can i do anything with it ? – Ehtesham Shareef Sep 05 '18 at 05:23
  • Yes you have to specify the path of the file, please read here for more details https://www.npmjs.com/package/request#multipartform-data-multipart-form-uploads – Gaurav joshi Sep 05 '18 at 05:28
  • I'm trying to do something like this : https://stackoverflow.com/questions/46975590/how-to-upload-file-saved-in-memory-by-multer-to-another-api?rq=1 – Ehtesham Shareef Sep 05 '18 at 05:37
  • I think this is what you exactly looking for https://stackoverflow.com/questions/25344879/uploading-file-using-post-request-in-node-js – Gaurav joshi Sep 05 '18 at 06:19
0

I solved it with the help of this post : how to upload file saved in memory by multer to another API

var stream = require('stream')

const { Duplex } = stream;
function bufferToStream(buffer) {
   const duplexStream = new Duplex();
   duplexStream.push(buffer);
   duplexStream.push(null);
   return duplexStream;
}

request.post({
       headers: { 'enctype': 'multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
       url: "http://localhost:7022/nuxeo/upload",
            formData: {
                file: {
                    value: bufferToStream(req.file.buffer),
                    options: {
                        filename: req.file.originalname,
                        contentType: req.file.mimetype,
                        knownLength: req.file.size
                    }
                },
                path: '/FCA/DT/clause32a'
            }
        }, function (error, response, body) {
            if (error) { return next(error) 
            res.send(JSON.parse(body))
        })