Question

I'd like to receive pictures, audio and video files with nodejs. I send them via phonegap as a http-request.

With nodeJS I use the connect plugin. I really don't understand what it does and how to manipulate the location the files are getting stored.

var http = require('http');
var connect = require('connect');

var app = connect();
var server = http.createServer(app);
app.use(connect.bodyParser());
app.use(function(req, res) {
    console.log(req.files); // Here is object with uploaded files
});
server.listen(8070);

How can I tell connect to store the files somewhere else as in the temp-directory.

And how can I read the requests options to decide where I want to store that file.

Here's what a file is about:

{ file:
{ fieldName: 'file',
 originalFilename: 'VID_20131211_124140.mp4',
 path: 'C:\\Users\\krause\\AppData\\Local\\Temp\\4120-1fx90bk.mp4',
 headers:
  { 'content-disposition': 'form-data; name="file"; filename="VID_20131211_124140.mp4"',
    'content-type': 'video/mp4' },
 ws:
  { _writableState: [Object],
    writable: true,
    domain: null,
    _events: [Object],
    _maxListeners: 10,
    path: 'C:\\Users\\krause\\AppData\\Local\\Temp\\4120-1fx90bk.mp4',
    fd: null,
    flags: 'w',
    mode: 438,
    start: undefined,
    pos: undefined,
    bytesWritten: 7046598,
    closed: true,
    open: [Function],
    _write: [Function],
    destroy: [Function],
    close: [Function],
    destroySoon: [Function],
    pipe: [Function],
    write: [Function],
    end: [Function],
    setMaxListeners: [Function],
    emit: [Function],
    addListener: [Function],
    on: [Function],
    once: [Function],
    removeListener: [Function],
    removeAllListeners: [Function],
    listeners: [Function] },
 size: 7046598,
 name: 'VID_20131211_124140.mp4',
 type: 'video/mp4' } }
Was it helpful?

Solution

I am assuming you just want to use this app to store the POST'ed file in a path other than tmp.

You can set the default upload directory by setting bodyParser. In Express we do it by app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));

You can try this is connect: app.use(connect.multipart({ uploadDir: path })); Check details here

Personally what I do is, I copy the file from temp directory and put it in a relevant place (if you have different directories for different uploads) using node fs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top