NITHIN SHAHRUKH
NITHIN SHAHRUKH

Reputation: 155

Pause/Resume file uploading to server from iphone

I am uploading video to server with HTTP posting.

How can I pause and resume this upload process by some button click

I am using the following code

  NSString *urlString =@"http://sampleurl.com/upload_video";

  NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"];


  [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", appDelegate.userid] dataUsingEncoding:NSUTF8StringEncoding]];
 [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


 [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", @"a.mov"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postbody appendData:[NSData dataWithData:file]];
 [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [request setHTTPBody:postbody];

 conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  if (conn) {
   webData = [[NSMutableData data] retain];
   }    

Upvotes: 2

Views: 2262

Answers (1)

Swati
Swati

Reputation: 1443

There is a connection delegate method which you can use

(void)connection:(NSURLConnection *)connection   didSendBodyData:(NSInteger)bytesWritten  totalBytesWritten:(NSInteger)totalBytesWritten   totalBytesExpectedToWrite (NSInteger)totalBytesExpectedToWrite;

You can store the total bytes sent when you PAUSE the connection and then send the rest of data bytes to the web service. But remember, your web service should also be capable of collecting the data and then creating an image from it.

Hope I helped.

Upvotes: 2

Related Questions