【发布时间】:2019-02-01 03:41:34
【问题描述】:
Busboy 是我用来上传文件的中间件。在 Chrome 中使用 html 表单,我可以上传文件(使用 'file' 事件),但是当 android 客户端尝试上传文件时,它不会触发 'file' 事件,而是触发 'field' 事件。
这里是我在服务器端使用的代码 sn-p:
import express from 'express';
import busboy from 'connect-busboy';
const app = express();
const busUpload = (req, res)=> {
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
saveTo = `${destination}/${filename}`;
Log('uploading to', saveTo);
file.pipe(fs.createWriteStream(saveTo));
// file is saved successfully.
});
req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
// I guess 'value' contains the file, but how do I save it? what is the name of file?
});
req.busboy.on('finish', function() {
Log('upload completed');
// res.writeHead(200, {'Connection': 'close'});
res.json({sucess: true});
});
// req.pipe(req.busboy);
};
app.use('/uploads', busboy({immediate: true}), busUpload)
有什么区别?我应该告诉 android 开发人员更改他的请求吗?或者如何将文件保存在“字段”事件的处理程序中?
【问题讨论】:
标签: node.js forms file-upload busboy