Thursday, 27 April 2017

Socket hang up Express and Nodejs

I am trying to upload a file to one of the folder in the sharefile so I am using their upload API method. I was able to compile a snippet to test whether the upload works but I keep getting socket hang up error. Can you guys please check the below code and let me know on what is it that I am doing wrong? And also if there is another way (easier or best practice) where I can upload a file

var url = ""+urlString;
var filePath = "D:/Analytics All Web Site Data Browser & OS 20161001-20170306.pdf";
var boundaryKey = Math.random().toString(16);

var r = request.post( {
  url: url,
  proxy: "http://localhost:3128"
}, function( err, res, data ) {
  console.log( "Finished uploading a file " + err + " :: " + res + " :: " + data );
});

r.setHeader('Content-Type', 'multipart/form-data; boundary="'+boundaryKey+'"');

r.write( 
  '--' + boundaryKey + '\r\n'
  + 'Content-Type: application/octet-stream\r\n' 
  + 'Content-Disposition: form-data; name="Analytics"; filename="Analytics All Web Site Data Browser & OS 20161001-20170306.pdf"\r\n'
);

fs.createReadStream('D:/Analytics All Web Site Data Browser & OS 20161001-20170306.pdf', { bufferSize: 1024 * 1024 })
  .pipe(r, { end: false })
  .on('end', function() {
    r.end('--' + boundaryKey + '--'); 
  });



via user3200361

No comments:

Post a Comment