通过HTTP POST发送大文件的正确方法是什么,而不将整个文件加载到ram中? [英] What is the correct way of sending large file through HTTP POST, without loading the whole file into ram?

查看:1825
本文介绍了通过HTTP POST发送大文件的正确方法是什么,而不将整个文件加载到ram中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正致力于通过简单的http帖子将大型视频文件从iPhone上传到网络服务的应用程序。截至目前,我在加载请求之前构建了NSURLRequest并预加载了所有视频文件数据。如果文件相当大,这自然会占用大量的内存,在某些情况下甚至不可能。

I'm currently working on an application for uploading large video files from the iPhone to a webservice through simple http post. As of right now, I build an NSURLRequest and preload all of the video file data before loading the request. This naturally eats a ton of ram if the file is considerably big, in some cases it's not even possible.

所以基本上我的问题是:是否有正确的流媒体方式数据或加载成块而不对网络服务器进行任何修改?

So basically my question is: Is there a correct way of streaming the data or loading it in chunks without applying any modifications to the webserver?

谢谢。

编辑澄清:我正在寻找一种方法来传输大型多部分/表格数据 FROM iPhone TO 一个网络服务器。不是另一种方式。

EDIT for clarification: I am searching for a way to stream large multipart/form data FROM the iPhone TO a webserver. Not the other way arround.

在接受答案后编辑:我刚刚发现苹果为此目的编写了一些漂亮的源代码,它显示向帖子附加附加数据,而不仅仅是大文件本身。任何人都需要它: SimpleURLConnections - PostController.m

EDIT after accepting answer: I just found out that apple has some nifty source code written for this exact purpose and it shows appending additional data to the post not just the big file itself. Incase anyone ever needs it: SimpleURLConnections - PostController.m

又一个编辑:使用来自苹果的源代码我遇到了一个非常愚蠢的甚至wireshark无法帮助我调试的丑陋问题。一些Web服务器在引号之间声明它时不理解边界字符串(如在apples示例中)。我在Apache Tomcat上遇到了问题并删除了引用效果非常好。

Yet another While using that piece of source code from apple I encountered a very stupid and ugly problem that even wireshark couldn't help me debug. Some webservers don't understand the boundary string when it's declared in between quotes (like in apples example). I had problems with it on Apache Tomcat and removing the quotes worked just wonderful.

推荐答案

你可以使用 NSInputStream on NSMutableURLRequest 。例如:

You can use NSInputStream on NSMutableURLRequest. For example:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
NSInputStream *stream = [[NSInputStream alloc] initWithFileAtPath:filePath];
[request setHTTPBodyStream:stream];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
}];

这篇关于通过HTTP POST发送大文件的正确方法是什么,而不将整个文件加载到ram中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆