问题描述
有很多关于如何使用多部分表单数据处理请求的帖子.但是我的用例是我有一个客户端,它期望来自服务器的多部分表单数据响应,我需要编写一个简单的 nodejs 服务器来测试我的客户端.
There are lots of posts about how to handle request with multipart form data . But my use case is that I have a client that expects multipart form data response from server, and I need to write a simple nodejs server in order to test my client.
为了编写简单的服务器,我有以下内容:
To write the simple server, I have the following:
var express = require('express');
var bodyParser = require('body-parser');
var FormData = require('form-data');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/describe', function(req, res) {
var form = new FormData();
form.append('part1', 'part 1 data');
form.append('part2', 'part 2 data');
res.setHeader('Content-Type', 'multipart/form-data');
res.send(form);
});
app.listen(3030, "0.0.0.0");
console.log('Listening on port 3030...');
现在当我的客户端请求 localhost:3030/describe 时,响应头显示以下内容,没有边界值
Now when my client request localhost:3030/describe, the response header shows the following without the boundary value
Content-Type: multipart/form-data; charset=utf-8
并且内容作为文件下载而不是在响应正文中.
And the content is downloaded as file instead of in the response body.
{"_overheadLength":208,"_valueLength":22,"_valuesToMeasure":[],"writable":false,"readable":true,"dataSize":0,"maxDataSize":2097152,"pauseStreams":true,"_released":false,"_streams":["----------------------------315683163006570790405079\r\nContent-Disposition: form-data; name=\"part1\"\r\n\r\n","part 1 data",null,"----------------------------315683163006570790405079\r\nContent-Disposition: form-data; name=\"part2\"\r\n\r\n","part 2 data",null],"_currentStream":null,"_boundary":"--------------------------315683163006570790405079"}
所以我的问题:1.如何让边界出现在响应头中?2.如何让表单数据响应内容显示在响应正文中而不是作为下载文件?
So my questions:1. how do we make the boundary appears in the response header?2. how do we make the form data response content show up in the response body instead as download file?
推荐答案
要发送表单数据,您需要对其进行管道传输(请参阅 文档),像这样:
To send your form data you'll want to pipe it (see documentation), like this:
form.pipe(res);
要将边界添加到标题中,您可以执行以下操作:
To add the boundary into the header, you can do something like this:
res.setHeader('Content-Type', 'multipart/form-data; boundary='+form.getBoundary());
现在,关于另存为"框:浏览器使用内容类型来确定如何处理文件.因此,如果您希望它显示在浏览器窗口中,一个不错的选择是 text/plain
(如果不起作用,也可能是 text/html
).然后你可以把你的 multipart/form-data
变成类似 x-content-type
的东西.
Now, about the "save as" box: The browser uses content-type to figure out what to do with the file. So if you want it to be displayed in the browser window, a good choice would be text/plain
(or possibly text/html
if that doesn't work). Then you could put your multipart/form-data
into something like x-content-type
.
(我假设最终您将使用 XHR 或 fetch,此时您可以将 content-type
切换回来.text/plain
事情只是让数据显示在标准 Web 浏览器中进行测试的临时解决方案.)
(I'm assuming eventually you'll be using XHR or fetch, and at that point you can switch the content-type
back. the text/plain
thing is just a temporary solution to get the data to show up in a standard web browser for testing.)
综合起来:
app.get('/describe', function(req, res) {
var form = new FormData();
form.append('part1', 'part 1 data');
form.append('part2', 'part 2 data');
res.setHeader('x-Content-Type', 'multipart/form-data; boundary='+form._boundary);
res.setHeader('Content-Type', 'text/plain');
form.pipe(res);
});
这篇关于创建一个使用多部分表单数据发送响应的 Nodejs 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!