6

I want to upload a large video from iPhone to a web server. I tried with below code. It's working for small files but when I try to upload large file it gets crashed.

Code:

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@"https://XXXXXXXXXXXXXXXX/submit.php"]];

NSData *webData = [NSData dataWithContentsOfURL:movieUrl];
NSString *postLength = [NSString stringWithFormat:@"%d", [webData length]];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

---- added some HTTP headers here ----

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[NSData dataWithData:webData]];
[request setHTTPBody:postbody];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

After research I got an idea as , this will be possible if I send large file in chunk of data. solution1 , solution2.

I also gone through Stream Programming apple doc. But don't know where to define server NSURL. After writing data into output stream how to send to the server.

If someone can point me to any working code then i will be able to understatnd it better.

How to do that? Have any idea?

Community
  • 1
  • 1
Kirti Nikam
  • 2,166
  • 2
  • 22
  • 43
  • 1
    Don't reinvent the wheel. Just use AFNetworking. It has functions to upload and download using streams. See here -> https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ#how-do-i-make-streaming-requests – borrrden May 13 '13 at 10:13
  • I have looked into AFNetworking also got an idea, But in example of Streaming Request, If I stored data into NSData then the app will get a crash because the video data is too large to hold in a NSData object at one time. – Kirti Nikam May 13 '13 at 10:45
  • ...then don't use NSData, that's what a streaming request means (streaming directly from a file). – borrrden May 14 '13 at 01:09
  • Hi, borrrden. Thank you for answering. Here, streaming directly from a file means what? Give me some working sample code. I am very much new to iOS. – Kirti Nikam May 14 '13 at 05:59
  • The link tells you what property to use. There are some samples also listed on the main page of that git hub. I don't have time to write sample code (which is why I didn't answer) but if you look at the documentation for the various classes (including NSInputStream) you should be able to work it out. – borrrden May 14 '13 at 06:05

0 Answers0