如何正确地将文件从 iPhone 发布到使用 node-formidable 的 node.js 服务器?

发布于 2024-11-02 01:58:45 字数 1705 浏览 3 评论 0原文

更新:此问题已于 2011 年 5 月 16 日在phonegap github 存储库中修复。


我有以下 Objective-C 代码:

NSData *photoData = [NSData dataWithContentsOfFile:photoPath];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%@\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:photoData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];

[[NSURLConnection alloc] initWithRequest:request delegate:appDelegate];

[photoData release];
[body release];
[request release];

我正在尝试将 iPhone 中的图像发布到 node.js 服务器那就是使用node-fordable。服务器已经可以正常处理从浏览器发布的图像,但是当我尝试从 iPhone 发布图像时,出现以下错误:

错误:MultipartParser.end():流 意外地结束于 MultipartParser.end (/formidable/1.0.0/package/lib/formidable/multipart_parser.js:301:12) 在IncomingMessage.anonymous (formidable/1.0.0/package/lib/formidable/incoming_form.js:80:30) 在传入消息.emit (events.js:61:17) 在 HTTPParser.onMessageComplete (http.js:132:23)位于 Socket.ondata (http.js:1001:22)位于 Socket._onReadable (net.js:677:27) 位于 IOWatcher.onReadable 作为回调 (net.js:177:10)

图像正确保存在服务器的临时文件夹中,但是它是节点强大的,在尝试解析图像时会抛出错误形式。

Update: this has been fixed in the phonegap github repository on May 16th, 2011.


I have the following Objective-C code:

NSData *photoData = [NSData dataWithContentsOfFile:photoPath];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%@\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:photoData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];

[[NSURLConnection alloc] initWithRequest:request delegate:appDelegate];

[photoData release];
[body release];
[request release];

I'm trying to post an image from an iPhone to a node.js server that is using node-formidable. The server is already working fine with images posted from a browser, but when I try to post from my iPhone I get the following error:

Error: MultipartParser.end(): stream
ended unexpectedly at
MultipartParser.end
(/formidable/1.0.0/package/lib/formidable/multipart_parser.js:301:12)
at IncomingMessage.anonymous
(formidable/1.0.0/package/lib/formidable/incoming_form.js:80:30)
at IncomingMessage.emit
(events.js:61:17) at
HTTPParser.onMessageComplete
(http.js:132:23) at Socket.ondata
(http.js:1001:22) at
Socket._onReadable (net.js:677:27) at
IOWatcher.onReadable as callback
(net.js:177:10)

The image is saved correctly on the server's temp folder, but it's node-formidable the one that throws an error when trying to parse the form.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-11-09 01:58:45

显然,强大对于最终的边界是相当挑剔的。试试这个:

 NSMutableData* body = [NSMutableData data];    
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"image/jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];

Apparently, formidable is quite picky with the end boundary. Try this:

 NSMutableData* body = [NSMutableData data];    
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"image/jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];
梦言归人 2024-11-09 01:58:45

我不知道到底是什么问题。我建议您安装一个工具,可以显示当您从浏览器和应用程序上传图像时这两个请求之间的差异。最著名的工具是 http://www.wireshark.org/
或者一些非免费的工具,例如httpscoop(http://www.tuffcode.com/download.html,14天试用)。我个人很喜欢他们两个。

一旦发现差异,您可以调整您的应用程序代码,使其发送完全相同的请求,这将解决您的问题。

I don't know what exactly the problem is. I suggest that you install a tool that can show you what the differences are between those two requests when you upload image from browser and your app. The most famous tool is http://www.wireshark.org/.
Or some unfree tools, such as httpscoop (http://www.tuffcode.com/download.html, 14 days trial). I personally like both of them.

Once you find the difference, you can adjust your app code to make it send exactly the same request, which will solve your problem.

空气里的味道 2024-11-09 01:58:45

[request release];

我猜想您在发送所有数据之前清理了请求对象。这可以解释意外的流退出。

[request release];

I would guess that your cleaning up the request object before it has send all the data. This would explain the unexpected stream exit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文