Node.js 使用 Express 在 HTTP POST 上以 multipart/form-data 形式写入 jpeg 文件

标签 node.js encoding http-headers url-encoding

我有一个 node.js 服务器正在从 iOS 应用程序接收内容。内容分为两部分:1.json和2.jpeg。我正在尝试将 jpeg 写入一个文件,然后可以在浏览器中提取该文件。这看起来很简单,但是当我将缓冲区从 req.body.imagedata 写入文件时,它是一个损坏的文件。

这是 iPhone 上 hexdump 的 jpeg 的第一部分

0000000 **ff d8 ff e0 00 10 4a 46 49** 46 00 01 01 00 00 01
0000010 00 01 00 00 ff e1 00 58 45 78 69 66 00 00 4d 4d
0000020 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01

这是 iPhone 模拟器的 Charles 代理输出。因此,它正在从 iPhone 正确传输。

00000060  69 6e 61 72 79 0d 0a 0d 0a **ff d8 ff e0 00 10 4a**   inary          J
00000070  **46 49** 46 00 01 01 00 00 01 00 01 00 00 ff e1 00   FIF             
00000080  58 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00   XExif  MM *     

这是服务器运行 Node 上的 tcpdump 输出。

0000370 **d8ff e0ff 1000 464a 4649** 0100 0001 0100
0000380 0100 0000 e1ff 5800 7845 6669 0000 4d4d
0000390 2a00 0000 0800 0200 1201 0300 0000 0100

服务器端 Node tcpdump 显示传入 Node.js 服务器的正确 header 信息。

?ò?
?u???????
8?8w
?u??????????%? ??
8?8??L?|?Q??
?u??????????%? O|
8?8??L?POST /screenw/ HTTP/1.1
Host: ec2-103-22-8-3.compute-1.amazonaws.com:4914
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=01947848929231
Cookie: connect.sid=s%3A3i1td3wlRo8dqrFETNTcbUBV.h4EPihMhJTOdlSBmIog3xm3C%2FvTWVyD4eBBw2Fige6o
Accept-Language: en-us
Accept: */*
Content-Length: 37297
Connection: keep-alive
User-Agent: Teachbits/1.0 CFNetwork/609.1.4 Darwin/12.2.0

|?Q??
?u???????%? ??
8?8??L?--01947848929231
Content-Disposition: form-data; name="imagedata"
Content-Transfer-Encoding: binary
....IMAGE DATA HERE.....

但是在node.js端写入的jpg的hexdump是错误的:

0000000 bfef efbd bdbf bfef efbd bdbf 1000 464a
0000010 4649 0100 0001 0100 0100 0000 bfef efbd
0000020 bdbf 5800 7845 6669 0000 4d4d 2a00 0000

以下是接收 POST 请求并写入 jpeg 文件的代码。

app.post('/screenw/', function (req, res, next) {

    require("fs").writeFile("./http_image_64.jpg", req.body.imagedata, function (err) {
                if (err) throw err;
                console.log('It\'s saved!');
            });

当我在 iPhone 模拟器和 Node 服务器上打印缓冲区的大小时,大小不匹配:

iPhone:图片内容长度为32661

node.js req.body.imagedata 原始图像 - 31679

我认为问题出在以下两个方面之一:

  1. Connect 如何解析 http 正文的 jpeg 部分并将其传回我的 server.js。即,我提供了错误的内容类型或其他 header 信息

  2. 我如何将 req.body.imagedata 写入 jpg 文件。即,我的编码和相关逻辑不正确。

关于如何让它发挥作用有什么想法吗?我被困住了。

[已编辑]

这里是构造http请求的objective-c代码:

//creating the url request:
tbImgUrl = [NSURL URLWithString:@"screenw/" relativeToURL:[cts web_service]];
NSMutableURLRequest *tbImgReq2 = [NSMutableURLRequest requestWithURL:tbImgUrl];
[tbImgReq2 setHTTPMethod:@"POST"];
NSString *boundary = @"01947848929231";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[tbImgReq2 setValue:contentType forHTTPHeaderField:@"Content-Type"];
[tbImgReq2 setValue:@"multipart/form-data" forHTTPHeaderField:@"enctype"];
NSData *imageData = UIImageJPEGRepresentation([imgVector screenImage], 1.0); // 0.0 - 1.0 l to h

// setting up the body
NSMutableData *body = [NSMutableData data];

// append the photo image
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imagedata\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", @"image/jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];

// append the json data
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"jsondata\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *reqData = [NSJSONSerialization dataWithJSONObject:sendPayload
                                                  options:0
                                                    error:nil];
[body appendData:[NSData dataWithData:reqData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[tbImgReq2 setHTTPBody:body];
tbImgConnection = [[NSURLConnection alloc] initWithRequest:tbImgReq2
                                                  delegate:self
                                          startImmediately:YES];

最佳答案

你可以使用express js ...

使用正确的编码规则提交,然后通过req.files通过node js(通过express库)访问,这对我来说非常可靠

关于Node.js 使用 Express 在 HTTP POST 上以 multipart/form-data 形式写入 jpeg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757116/

相关文章:

c# - 内容处置文件名不适用于 IE

angularjs - 请求中的请求头和 cookie 中的 CSRF token 不匹配

node.js - NodeJS 需要模块无法工作

node.js - 从 Node 运行 Mocha 测试 - 'describe' 未定义

javascript - 符号是如何工作的?

email - grails邮件中的主题字段编码

javascript - ldapjs 身份验证(用户登录设置)

python - 将字节写入文件,编码错误

xml - 使用 Linux 命令将纯字符串转换为 xml 格式

html - 缓存控制 HTML header