node.js 在 Express.js 应用上上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29746727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:39:24  来源:igfitidea点击:

Upload file on Express.js app

node.jsfile-uploadexpress

提问by Luca Marzi

I'm developing a RESTful API for a mobile client application with the combination of Node.js, Express.js and Mongodb. Now I'm trying to handle the upload of the user profile image and I've found a module called "multer" (that is the one ecommended by express.js team itself) that allow the express.app to handle multipart/form-data requests. Now I need to test the app and, moreover, the upload function but I'm not able to simulate a http-form request (via postman chrome plugin). Multer returns this error:

我正在为移动客户端应用程序开发一个 RESTful API,结合 Node.js、Express.js 和 Mongodb。现在我正在尝试处理用户个人资料图片的上传,我发现了一个名为“multer”的模块(这是 express.js 团队本身推荐的模块),它允许 express.app 处理 multipart/form-数据请求。现在我需要测试应用程序以及上传功能,但我无法模拟 http 表单请求(通过 postman chrome 插件)。Multer 返回此错误:

[Error: Multipart: Boundary not found]

[错误:多部分:未找到边界]

In fact, comparing an http-form request (which works) with a custom http request, the second one has not the Boundary header property.

实际上,将 http 表单请求(有效)与自定义 http 请求进行比较,第二个请求没有 Boundary 标头属性。

What Boundary property is?

什么是边界属性?

采纳答案by Plato

Boundary in a multipart form indicates some delimiter string separating text and binary data. You can do this in postman but it sounds like you aren't sending both file and text so postman maybe defaults to a regular form. do you see something like:

多部分形式的边界表示一些分隔文本和二进制数据的分隔符字符串。您可以在邮递员中执行此操作,但听起来您不会同时发送文件和文本,因此邮递员可能默认为常规形式。你看到类似的东西:

postman-form-multipart

邮递员形式多部分

If you click preview in postman you can see the boundary in the Content-type header and in the body.

如果您在 postman 中单击预览,您可以在 Content-type 标题和正文中看到边界。

回答by Xelz

If you are using Postman, you can try removing the Header: "Content-type": "multipart/form-data". I removed it and now it works.

如果您使用的是 Postman,您可以尝试删除 Header:"Content-type":"multipart/form-data"。我删除了它,现在它可以工作了。

回答by punith ck punith

solutions:

解决方案:

1) don't specify the content-typeat client

1) 不指定content-typeat 客户端

2) use the naming convention(imageUpload)in upload.single('imageUpload')same as field name

2)使用的命名convention(imageUpload)upload.single('imageUpload')相同的字段名