javascript 节点js在HTTP请求中接收文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46972607/
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
Node js receive file in HTTP request
提问by HowDoYouTellForAChild
I try to create a server, which can receive a file from an HTTP request. I use Postman as user agent and I add a file to the request. This is the request:
我尝试创建一个服务器,它可以从 HTTP 请求接收文件。我使用 Postman 作为用户代理,并向请求添加一个文件。这是请求:
POST /getfile HTTP/1.1
Host: localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 9476dbcc-988d-c9bd-0f49-b5a3ceb95b85
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="test.xls"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundary7MA4YWxkTrZu0gW--
But when the request reaches the server I can not find the file in it (I mean in the request).
I tried to receive it from the body part of the request, but it returned > {}<. I tried to figure out, how can I refer to the name of the file, but unfortunately I can not find any reference in the request header for the name of the file...
但是当请求到达服务器时,我在其中找不到文件(我的意思是在请求中)。我试图从请求的正文部分接收它,但它返回了 > {}<。我试图弄清楚,如何引用文件名,但不幸的是我在请求头中找不到文件名的任何引用......
Can anybody help me to find out, what should I do?
谁能帮我查一下,我该怎么办?
回答by stavros.zavrakas
As a follow up to my comment, you can use the multer module achieve the thing that you want: https://www.npmjs.com/package/multer
作为我评论的后续,您可以使用 multer 模块实现您想要的东西:https://www.npmjs.com/package/multer
const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer();
app.post('/profile', upload.array(), function (req, res, next) {
// req.body contains the text fields
});
回答by Adrian Zhang
var app = require('express')();
var multer = require('multer');
var upload = multer();
app.post('/your_path', upload.array(), function (req, res, next) {
// req.files is array of uploaded files
// req.body will contain the text fields, if there were any
});
回答by Sunspots
You need to parse the form data from the request. There are a few packages that solves this problem, notably formidable, busboy(or busboy-connect), partedand flow.
您需要从请求中解析表单数据。有一些软件包可以解决这个问题,特别是formidable, busboy(或busboy-connect),parted和flow.
Here's a solution using formidable, it is my preferred solution for things like image uploads because it saves to disk. If you just want to read the file, you can use one of the other packages above.
这是一个使用formidable的解决方案,它是我首选的图像上传等解决方案,因为它保存到磁盘。如果您只想读取文件,可以使用上面的其他软件包之一。
Install formidable
安装强大
npm install formidable --save
npm install formidable --save
Then, in your server, you will have to parse the data from the client:
然后,在您的服务器中,您必须解析来自客户端的数据:
// Somewhere at the start of your file
var IncomingForm = require('formidable').IncomingForm
// ...
// Then in your request handler
var form = new IncomingForm()
form.uploadDir = 'uploads'
form.parse(request, function(err, fields, files) {
if (err) {
console.log('some error', err)
} else if (!files.file) {
console.log('no file received')
} else {
var file = files.file
console.log('saved file to', file.path)
console.log('original name', file.name)
console.log('type', file.type)
console.log('size', file.size)
}
})
A few things to note:
需要注意的几点:
- formidable saves files with new names, you can use
fsto rename or move them - you can set
form.keepExtensions = trueif you want saved files to keep their extensions
- 强大的以新名称保存文件,您可以使用它
fs来重命名或移动它们 - 您可以设置
form.keepExtensions = true是否希望保存的文件保留其扩展名

