2013-06-18 9 views
0

我从2天卡住了,我想要显示downlaod进度条。 我发送json发送到服务器,并在响应服务器发送给我的视频数据。 要显示进度条我写像如何将数据存储在did-receive-data中的NS-Mutable-data中,然后写入文件

一些逻辑代码在ASIhttep我正在与附加全球NSmutabledata和在请求做方法接收数据didreceivedata方法我写,全球Nsmutalbedata成文件。 但文件是空白的,它不会得到存储到文件。

我知道ASIHttprequest是旧图书馆,但每个人都建议我使用AFnetworking,但我不想更改代码,因为它会花费很多时间,我必须再次阅读文档。

任何人都可以帮助我如何追加数据,并在downlaod完成后将附加数据写入文件?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://testing.io/dev.php/video/verifyReceipt"]]; 

    [request setDidReceiveDataSelector:@selector(request:didReceiveData:)]; 
    [request setPostValue:resultAsString forKey:@"verify"]; 
    [request setDidFinishSelector:@selector(requestDone:)]; 
    [request setTimeOutSeconds:120]; 
    [request setDelegate:self]; 
    [request setNumberOfTimesToRetryOnTimeout:2]; 
    [request setDownloadProgressDelegate:progressBar]; 
    request.showAccurateProgress = YES; 

    [request startSynchronous]; 
    } 
    -(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 
    { 
      [videoData appendData:data]; 
      NSLog(@"data is %@",data); 
    } 

    - (void)requestDone:(ASIHTTPRequest *)request 
    { 
    //[MBProgressHUD hideHUDForView:self.view animated:YES]; 

    // SAVED PDF PATH 
    // Get the Document directory 
     NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    // Add your filename to the directory to create your saved pdf location 
    NSString* movLocation = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]]; 

    if(request.responseStatusCode==200) 
    { 
     [videoData writeToFile:movLocation atomically:NO]; 
     NSLog(@"in request done sucsessfully downlaod and store in database %d",request.responseStatusCode); 
     [DBHelper savePurchaseId:fileName]; 
     [self movieReceived]; 
    } 
    else 
    {   
     NSLog(@"in request downlaod and store in database failed %@",request.responseHeaders); 

    } 
} 
+0

问题是什么?我想象一下,在你的情况下,videoData是一个NSMutableData,所以在dowload中你可以在iPhone磁盘上写文件,就像你做的那样...当你说追加意味着你有一个部分下载并且你想恢复下载并将数据附加到现有文件? – Manu

+0

@Manu是的,我想追加旧的数据与新的接收数据,并在完成后将其写入文件...但我的文件没有创建..我只是想显示进度条的下载..所以这就是我正在使用didreceivedata通过使用,我可以显示多少数据,我得到并更新progres酒吧..我使用HUD进度条不是简单的进度条 –

回答

0

对这样的任务使用异步请求会更好。您可以使用相同的ASIHTTPRequest类,但采用块方式。尝试编写类似于此的代码:

-(void) verifyReceipt { 
    NSURL *theURL = [NSURL URLWithString:@"http://testing.io/dev.php/video/verifyReceipt"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f]; 
[theRequest setHTTPMethod:@"POST"]; 

    NSString *param1 = [self getParam1]; // getParam1 - some method to get useful data for request's body 
    NSNumber *param2 = [self getParam2]; 
    NSString *postString = [NSString stringWithFormat:@"param1=%@&param2=%@", param1, param2]; 

    [theRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     if ([data length] > 0 && error == nil) { 
      //[delegate receivedData:data]; // - if you want to notify some delegate about data arrival 
      NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString *filePath = [NSString stringWithFormat:@"%@/fileArrived.ext", rootPath]; 

      //try to access that local file for writing to it... 
      NSFileHandle *hFile = [NSFileHandle fileHandleForWritingAtPath:filePath]; 
      //did we succeed in opening the existing file? 
      if (!hFile) 
      { //nope->create that file! 
       [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
       //try to open it again... 
       hFile = [NSFileHandle fileHandleForWritingAtPath:filePath]; 
      } 
      //did we finally get an accessable file? 
      if (!hFile) 
      { //nope->bomb out! 
       NSLog(@"could not write to file %@", filePath); 
       return; 
      } 
      //we never know - hence we better catch possible exceptions! 
      @try 
      { 
       //seek to the end of the file 
       [hFile seekToEndOfFile]; 
       //finally write our data to it 
       [hFile writeData:data]; 
      } 
      @catch (NSException * e) 
      { 
       NSLog(@"exception when writing to file %@", filePath); 
      } 
      [hFile closeFile]; 
     } else if ([data length] == 0 && error == nil) { 
      //   [delegate emptyReply]; 
     } else if (error != nil && error.code == NSURLErrorTimedOut) { 
      //   [delegate timedOut]; 
     } else if (error != nil) { 
      //   [delegate downloadError:error]; 
     } 
     [queue release]; 
    }]; 
} 

这会将每个到达的大数据块添加到文件中,如您所愿。 为您的需求自定义请求POST正文,这应该工作。异步:)

+0

你代码创建文件,但不存储我得到的数据,在请求的响应... –

+0

它是来自我的一个项目的工作代码。所以请求主体或数据到达可能有问题。由于您的代码创建文件,因此* [数据长度]> 0 *为true。尝试在if语句正文中插入* NSLog(@“%@”,data); *,看看输出是什么。也许你的服务器提供了一些可理解的错误信息 – makaron

0

好吧,首先检查你的文件的路径,我通常喜欢引用文件路径是这样的:

你需要让你的应用程序的根以这样的方式

NSString* rootPath = NSHomeDirectory(); 

并通过Apple file system guide line

NSString* fullPath = [rootPath stringByAppendingPathComponent:@"subFoldeder/file.extension"]; 

指定关于新数据添加到旧的数据非常快速的解决方案可能是initiali保存在子文件中的一个数据泽你这样可视数据:

NSMutableData *videoData = [[NSMutableData alloc] initWithContentsOfFile:@"filePath"]; 

之后,你可以PROCEDE像你已经史稿您收到的附加数据,并写在最后

做正确的事情完整的文件,以不要使用太多的内存,应该打开一个文件,将其查找到最后并将数据追加到文件中

相关问题