TypeError:无法读取Node.js中未定义的属性'mv'

aiqt4smr  于 2023-01-12  发布在  Node.js
关注(0)|答案(3)|浏览(181)

My Node.js code:

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!');
  });
});

The 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: Cannot read property 'mv' of undefined at C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\server.js:19:14 at Layer.handle [as handle_request] (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\layer.js:95:5) at C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:335:12) at Busboy.next (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:275:10) at emitNone (events.js:111:20) at Busboy.emit (events.js:208:7)
the req.file contains the image but when I read the the .mv() function it is saying undefined .

qyuhtwio

qyuhtwio1#

根据console.log,我认为结构应该是

let sampleFile = req.files['uploads[]'];
sampleFile.mv();

它看起来像req.files = { 'uploads[]': { mv } },而不是req.files = [{ mv }]

isr3a4wc

isr3a4wc2#

使用***快速文件加载***

npm install express-fileupload
const fs = require('fs');
const app = express();
const fileUpload= require("express-fileupload");
app.use(fileUpload());

app.post('/upload/', (req, res, next) => {

  console.log(req.files); // To Check whether you received the file

  const file = req.files.xxxImage;
  const fileName = req.files.xxxImage.name;
  console.log(fileName);
  const  path = 'C:/Users/Project/client/public/Image/';

  // If you don't want to consider the error enable this line
  // file.mv(path + fileName);

  file.mv(path + fileName, function (err) {
    if (err) {
     return res.send(err);
    }else{
      res.send('File uploaded');
    }
  });
});
js4nwp54

js4nwp543#

在这里,您需要指定视图引擎或html输入元素的name属性中给出的文件名

let sampleFile = req.files.attributename[0];

相关问题