如何正确地将文件从 iPhone 发布到使用 node-formidable 的 node.js 服务器?
更新:此问题已于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,强大对于最终的边界是相当挑剔的。试试这个:
Apparently, formidable is quite picky with the end boundary. Try this:
我不知道到底是什么问题。我建议您安装一个工具,可以显示当您从浏览器和应用程序上传图像时这两个请求之间的差异。最著名的工具是 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.
[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.