OGeek|极客世界-中国程序员成长平台

标题: iphone - 暂停/恢复从 iphone 上传到服务器的文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:39
标题: iphone - 暂停/恢复从 iphone 上传到服务器的文件

我正在通过 HTTP 发布将视频上传到服务器。

如何通过单击按钮来暂停和恢复此上传过程

我正在使用以下代码

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

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


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



Best Answer-推荐答案


有一个可以使用的连接委托(delegate)方法

(void)connectionNSURLConnection *)connection   didSendBodyDataNSInteger)bytesWritten  totalBytesWrittenNSInteger)totalBytesWritten   totalBytesExpectedToWrite (NSInteger)totalBytesExpectedToWrite;

您可以存储暂停连接时发送的总字节数,然后将剩余的数据字节发送到网络服务。但请记住,您的网络服务也应该能够收集数据,然后从中创建图像。

希望我能帮上忙。

关于iphone - 暂停/恢复从 iphone 上传到服务器的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401941/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4