【问题标题】:multer single upload doesnot work but array worksmulter 单次上传不起作用,但数组起作用
【发布时间】:2021-08-24 05:46:01
【问题描述】:

我正在使用 react js 上传单个文件。但是 multer.single() 不起作用,而 multer.array() 起作用。有什么问题?

//Node.js
const upload = multer({
    storage: multer.diskStorage({
        destination: './uploads',
        filename: (_, file, cb) => cb(null, file.originalname),
    }),
});
// app.use(upload.array('pdf')); // works
app.use(upload.single('pdf')); // not working

使用单一方法时的错误是

(node:6096) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at convertToJPG (D:\Projects\Node\pdf2pic\controllers\convert.js:9:44)
    at Layer.handle [as handle_request] (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\layer.js:95:5)
    at D:\Projects\Node\pdf2pic\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Projects\Node\pdf2pic\node_modules\express\lib\router\index.js:47:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6096) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6096) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

客户端代码是

<input
          accept=".pdf"
          multiple={false}
          name="pdf"
          onChange={onChangeHandler}
          ref={fileInputRef}
          style={{ display: 'none' }}
          type="file"
        />

    const onChangeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
      if (!event.target.files?.length) {
        return;
      }
  
      const formData = new FormData();  
      Array.from(event.target.files).forEach((file) => {
        formData.append(event.target.name, file);
      });
  
      props.onChange(formData);
  
      formRef.current?.reset();
    };

我在这里做错了什么?

【问题讨论】:

  • D:\Projects\Node\pdf2pic\controllers\convert.js:9:44 的代码是什么?您可能正在尝试在那里访问 req.files 数组,但使用 single 时没有数组,而只有 req.file 属性。 (注意 filesfile 的区别)

标签: javascript node.js reactjs multer


【解决方案1】:

两者都像路由器中的中间件一样使用

const uploadFiles = multer({'./images}).single("profile"); const uploadtext = multer().array()

【讨论】:

    【解决方案2】:

    使用 multer 处理单个文件和多个文件上传的主要区别在于文件的位置:

    • upload.single() -> req.file
    • upload.array()(和upload.fields())-> req.files

    如果不进一步考虑代码中的此更改,您就无法从多个交换到单个。

    从您的错误消息来看,在 convert.js 文件中(第 9 行第 44 列)您未能读取数组 req.files 的第一个元素。那是因为 req.files 不存在,您应该使用 req.file 而不是它是一个对象。

    Parsing Requests in Node.js 的免费指南将帮助您在 Node.js 中处理文件上传时修复这些类型的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-09
      • 2016-04-11
      • 2015-03-09
      • 2021-10-17
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多