在Express.js应用上上传文件 [英] Upload file on Express.js app

查看:57
本文介绍了在Express.js应用上上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js,Express.js和Mongodb的组合为移动客户端应用程序开发RESTful API. 现在,我正在尝试处理用户个人资料图片的上传,并且找到了一个名为"multer"的模块(这是express.js团队本身所推荐的模块),该模块允许express.app处理多部分/表单-数据请求. 现在,我需要测试应用程序以及上传功能,但是我无法模拟http形式的请求(通过邮递员chrome插件). Multer返回此错误:

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:

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

实际上,将一个http形式的请求(有效)与一个自定义的http请求进行比较,第二个请求没有Boundary标头属性.

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

什么是边界属性?

推荐答案

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

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:

如果单击邮递员中的预览,则可以在Content-type标头和正文中看到边界.

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

这篇关于在Express.js应用上上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆