简体   繁体   English

暂停/恢复文件从iPhone上载到服务器

[英]Pause/Resume file uploading to server from iphone

I am uploading video to server with HTTP posting. 我正在通过HTTP发布将视频上传到服务器。

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. 您可以存储在暂停连接时发送的总字节数,然后将其余数据字节发送至Web服务。 But remember, your web service should also be capable of collecting the data and then creating an image from it. 但是请记住,您的Web服务还应该能够收集数据,然后从中创建图像。

Hope I helped. 希望我能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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