简体   繁体   English

使用Connect通过NodeJS接收文件

[英]Receiving Files with NodeJS using connect

I'd like to receive pictures, audio and video files with nodejs. 我想使用nodejs接收图片,音频和视频文件。 I send them via phonegap as a http-request. 我通过phonegap将它们作为http请求发送。

With nodeJS I use the connect plugin. 使用nodeJS时,我使用connect插件。 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. 我该如何告诉connect将文件存储在temp目录中的其他位置。

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' } }

I am assuming you just want to use this app to store the POST'ed file in a path other than tmp . 我假设您只是想使用此应用程序将POST的文件存储在tmp以外的路径中。

You can set the default upload directory by setting bodyParser. 您可以通过设置bodyParser来设置默认的上传目录。 In Express we do it by app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' })); 在Express中,我们通过app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));做到这一点app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));

You can try this is connect: app.use(connect.multipart({ uploadDir: path })); 您可以尝试使用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 . 我个人所做的是,我使用节点fs从temp目录中复制文件,并将其放在相关的位置(如果您具有用于不同上载的不同目录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM