0

File uploading using multer is not happening

My code:

File read from html and pass to external url

router.post('/fileUpload',function (req,res) {
   request({
             uri: http//example.com/upload,  // url of other server
             method: "POST",
             form: {
                   "name":req.body.name,
                   "image":? //image from html - no idea how to pass the req.files here
                   }
                    }, function (error, response, body) {
                        ------------
                       ------------
                        }
                });

other server url : /example.com/upload

This is the code to upload image using multer

app.post('/upload',function(req,res){

    var storage =   multer.diskStorage({
    destination: function (req, file, callback) {

      callback(null, 'uploads');
    },
    filename: function (req, file, callback) {
      callback(null, file.fieldname + '-' + Date.now());
    }
});

    var upload = multer({ storage : storage }).array('productImage');
        upload(req,res,function(err) {

            if(err) {
                return res.json({'success':0,'result':{},'errorMessage':'Unknown Error'});
            }
            return res.json({'success':1,'result':{},'errorMessage':''});
        });
    });
midhun k
  • 1,032
  • 1
  • 15
  • 41

2 Answers2

2

Create readStream from file uploaded and pipe it to the another server.check this link https://www.npmjs.com/package/request, you will easily get this done.

Karthik
  • 284
  • 2
  • 4
  • 12
0

The simple answer would be to create a read stream from the uploaded file and pipe it to the second server, like so:

fs.createReadStream(req.files[0].path).pipe(request.post('http//example.com/upload'))

However, there are many ways to make this work. The most efficient of which is to use a binary stream from the initial upload all the way to the second server. You want to avoid using your first server as a storage for all of the uploads.

Here's how I would do it instead:

Use jQuery file upload on client side upload (Or any other library/approach to upload the raw binary stream) $('#fileupload').fileupload({ url: 'https://server1.com/upload' })

Use Formidable (or other library like multer) to handle upload server-side (This will allow you to read the binary stream in parts, and handle each part as it comes) app.post('/upload',function(req,res){ var form = new formidable.IncomingForm();

    form.parse(req);

    form.onPart = function(part) {
        fs.createReadStream(part).pipe( request.post('http//example.com/upload'))
    }
}

When each part of the binary upload is received, you can to stream the binary directly to the second server via pipe() to avoid having to store it on the first server whatsoever.

The key to making this work is to look at the file upload and transfer as a stream of binary bits. When the user uploads a file (req.body) you want to create a read stream from the file, and pipe the binary over the request.post().

Reza Karami
  • 505
  • 1
  • 5
  • 15