简体   繁体   中英

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];
   }    

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM