Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: sending/uploading a local file to a remote server

Tags:

I have used the Winston module to create a daily log file for my offline app. I now need to be able to send or upload that file to a remote server via POST (that part already exists)

I know I need to write the file in chunks so it doesn't hog the memory so I'm using fs.createReadStream however I seem to only get a 503 response, even if sending just sample text.

EDIT

I worked out that the receiver was expecting the data to be named 'data'. I have removed the createReadSteam as I could only get it to work with 'application/x-www-form-urlencoded' and a synchronous fs.readFileSync. If I change this to 'multipart/form-data' on the php server would I be able to use createReadStream again, or is that only if I change to physically uploading the json file.

I've only been learning node for the past couple of weeks so any pointers would be gratefully received.

var http = require('http'),     fs = require('fs');  var post_options = {     host: 'logger.mysite.co.uk',     path: '/',     port: 80,     timeout: 120000,     method: 'POST',     headers: {         'Content-Type': 'application/x-www-form-urlencoded'     } }  var sender = http.request(post_options, function(res) {     if (res.statusCode < 399) {         var text = ""         res.on('data', function(chunk) {             text += chunk         })         res.on('end', function(data) {             console.log(text)         })     } else {         console.log("ERROR", res.statusCode)     } })  var POST_DATA = 'data={[' POST_DATA += fs.readFileSync('./path/file.log').toString().replace(/\,+$/,'') POST_DATA += ']}' console.log(POST_DATA) sender.write(POST_DATA) sender.end() 
like image 972
sidonaldson Avatar asked Nov 06 '13 17:11

sidonaldson


People also ask

How do I send a file in node JS body?

To upload a file using POST request in Node. js, we can submit our files in a form data object. const req = request. post(url, (err, resp, body) => { if (err) { console.


2 Answers

After gazillion of trial-failure this worked for me. Using FormData with node-fetch. Oh, and request deprecated two days ago, btw.

const FormData = require('form-data'); const fetch = require('node-fetch');  function uploadImage(imageBuffer) {   const form = new FormData();   form.append('file', imageBuffer, {     contentType: 'image/jpeg',     filename: 'dummy.jpg',   });   return fetch(`myserver.cz/upload`, { method: 'POST', body: form }) }; 

In place of imageBuffer there can be numerous things. I had a buffer containing the image, but you can also pass the result of fs.createReadStream('/foo/bar.jpg') to upload a file from drive.

like image 175
kub1x Avatar answered Oct 03 '22 09:10

kub1x


copied from https://github.com/mikeal/request#forms

var r = request.post('http://service.com/upload', function optionalCallback (err, httpResponse, body) {   if (err) {     return console.error('upload failed:', err);   }   console.log('Upload successful!  Server responded with:', body); }) var form = r.form() form.append('my_field1', 'my_value23_321') form.append('my_field2', '123123sdas') form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png'))) 
like image 38
waqas Avatar answered Oct 03 '22 11:10

waqas