我想用nodejs接收图片、音频和视频文件。 我通过phonegap 将它们作为http 请求发送。
对于nodeJS,我使用连接插件。我真的不明白它的作用以及如何操纵文件存储的位置。
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);
如何告诉 connect 将文件存储在其他位置(例如临时目录中)。
如何读取请求选项来决定将该文件存储在何处。
以下是文件的含义:
{ 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' } }
最佳答案
我假设您只想使用此应用将 POST 后的文件存储在 tmp
以外的路径中。
您可以通过设置bodyParser来设置默认上传目录。
在 Express 中,我们通过以下方式做到这一点
app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));
您可以尝试这样连接:
app.use(connect.multipart({ uploadDir: path }));
查看详情here
我个人所做的是,我使用 node fs 从临时目录复制文件并将其放在相关位置(如果您有不同的目录用于不同的上传) .
关于node.js - 使用 NodeJS 连接接收文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379732/