0

我正在从响应中下载视频文件。我想显示HUD-progress的下载进度条。但我该怎么做。我将验证 json 发送到服务器,服务器验证发送回视频文件字节。我想显示使用 HUD-progreasse 栏完成了多少百分比的下载。

如果我打电话[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; 比它显示我得到了多少字节,但它不会将字节存储到缓存文件中(它不会将文件存储到手机中)

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://testing.io/dev.php/video/verifyReceipt"]];
  [request setPostValue:resultAsString forKey:@"verify"];// sending json in post 
[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; 
[request setDidFinishSelector:@selector(requestDone:)];
[request setTimeOutSeconds:120];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadProgressDelegate:self];
request.showAccurateProgress = YES;

[request startSynchronous];

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

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

// SAVED Video PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved video 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);

}
 }
     -(void)requestFailed:(ASIHTTPRequest *)request
      {

NSLog(@"%@",request.error);

      }
4

2 回答 2

0

fileUrl = 下载视频的 URL 字符串。

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    AFHTTPRequestOperation *操作 = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO];
    [操作 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        // 提示下载成功。
        NSLog(@"成功下载文件到%@", destPath);

        /* 调用委托来响应结果。*/
        [self.target parserDidDownloadItem:destPath];

        HUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",[JGlobals getLocalvalue:@"Downloading"],100];
        [HUD隐藏:真];
    }
    失败:^(AFHTTPRequestOperation *操作,NSError *错误)
    {
        // 提示下载失败
        NSLog(@"错误:%@", 错误);

        /* 调用委托来响应结果。*/
        [self.target parserDidFailToDownloadItem:error];
        [HUD隐藏:真];

    }];
    [操作 setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
    {
         // 进步
         float totalProgress = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
         HUD.detailsLabelText = [NSString stringWithFormat:@"正在下载 %i%%", MIN((int)(totalProgress), 99)];
     }];
    [操作开始];
于 2013-06-17T12:03:27.507 回答
0

我正在考虑一种可能不是正确的方法,但它可以让您免于编写大量代码。

首先UIProgressView像这样设置一个普通实例作为下载进度委托

[request setDownloadProgressDelegate:uiprogreeViewInstance];

现在,ASIHTTPRequest 框架将负责在下载发生时更新该进度视图。ProgressView 有一个名为 的属性progress,范围从 0.0 到 1.0。

现在实例化 MBProgressHUD,将其添加为您想要的子视图。将实例的progress值设置为. 隐藏 uiprogressview 实例。MBProgressHUDUIProgressView

于 2013-06-17T12:33:06.503 回答