使用连接接收文件与NodeJS

我想用nodejs接收图片,audio和video文件。 我通过手机发送它们作为http请求。

使用nodeJS我使用connect插件。 我真的不明白它的作用,以及如何操纵文件存储的位置。

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); 

我怎么能告诉连接到文件的其他地方存储在临时目录中。

我怎样才能读取请求选项,以决定我要存储该文件的位置。

这是一个文件是关于:

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

我假设你只是想使用这个应用程序存储在tmp以外的pathPOST'ed文件。

您可以通过设置bodyParser来设置默认上传目录。 在Express中,我们通过app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));

你可以试试这个是connect: app.use(connect.multipart({ uploadDir: path })); 在这里查看详情

就我个人而言,我从临时目录复制文件,并使用节点fs把它放在相关的地方(如果你有不同的上传目录)。