Wednesday 17 May 2017

NodeJS file upload to server

I have a nodeJS server running express that takes a file passed to it in the body and uploads it to a location and then does some things with it. This part works as iv tested it with Advanced Rest Client.

Im now trying to make a nodejs program so that i dont have to use Advanced rest client and can just do this process by myself. I get a response so thats fine but the file is not being uploaded to the location it should be which means the response is wrong.

var req = require('request');
var fs = require('fs');

var express = require("express")
var app = express()

var myFile = 'My File Location'
var file = fs.createReadStream(myFile)
var options = {
  headers: {'content-type' : 'application/x-www-form-urlencoded'},
  url:     'http://localhost:8080/api/file',
  form: {'filename' : 'fileUpload'
        'body' : file}
}

req.post(options, function(error, res, form){
  console.log(res)
});

I figure it has to do with the form. I can pass a plain text as the body if i change the form:{} to body:'hello world' but i need to specify a filename so that i can call that with .single('file-name') in my express server



via ld436

No comments:

Post a Comment