My Node.js代码:
app.use(fileUpload());
app.post('/upload', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
console.log(req.files);
let sampleFile = req.files[0];
// Use the mv() method to place the file somewhere on your server
sampleFile.mv('C:/Users/MNaaraayanan/Downloads/boilerplate_new/src/main/ngapp/', function(err) {
if (err)
return res.status(500).send(err);
res.send('File uploaded!');
});
});
console.log()
:
{ 'uploads[]':
{ name: 'blank_user.png',
data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 fb 00 00 00 fb 08 02 00 00 00 23 10 75 f1 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 ... >,
encoding: '7bit',
truncated: false,
mimetype: 'image/png',
md5: '0c803e9cef05aedeada6fb9587f1b073',
mv: [Function: mv] } }
TypeError:无法读取未定义的属性“mv” 在C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ server.js:19:14 在Layer.handle [as handle_request](C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [as handle_request](C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ layer.js:95:5) 在C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ index.js:281:22 在Function.process_params(C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ index.js:335:12) 在Busboy.next(C:\ Users \ MNaaraayanan \ Downloads \ boilerplate_new \ src \ main \ ngapp \ node_modules \ express \ lib \ router \ index.js:275:10) 在emitNone(events.js:111:20) 在Busboy.emit(events.js:208:7)
req.file
包含图片,但当我阅读.mv()
函数时,它会说undefined
。
答案 0 :(得分:1)
在你的console.log之后,我认为结构应该是
let sampleFile = req.files['uploads[]'];
sampleFile.mv();
看起来像req.files = { 'uploads[]': { mv } }
,而不是req.files = [{ mv }]