【问题标题】:How do I get the file size to know whether it's download or not?如何获取文件大小以知道它是否下载?
【发布时间】:2013-04-18 12:08:40
【问题描述】:

我正在使用以下代码从 url 下载 epub /pdf。我喜欢提供一个进度条,所以当我开始下载时它会显示进度,下载完成时会弹出一条消息。我该如何实现?

我的下载文件代码

-(void)Download
 {
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL    URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];

    //Store the Data locally as epub  File if u want pdf change the file extension  

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];

    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
 }

我在我的 .m 文件中使用此代码,但它不适合我

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _totalFileSize = response.expectedContentLength;
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    _receivedDataBytes += [data length];
    MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
    [responseData appendData:data];
}

【问题讨论】:

标签: ios objective-c progress-bar download file-handling


【解决方案1】:

使用 NSURLConnection

在 .h 文件中

double datalength;
NSMutableData *databuffer;
UIProgressView *progress;

在 .m 文件中

-(void)Download
{
      NSURLConnection *con=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]] delegate:self startImmediately:YES];
      [con start];
}

委托方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    datalength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [databuffer appendData:data];
    progress.progress = (databuffer.length/datalength);
    self.HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading  %.f  %%",(databuffer.length/datalength)*100];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];
    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
}

【讨论】:

  • 在 appdelege.m 文件 ryt a 中委托方法
  • naah.. 在 yourViewController.h 中??如果您从 appDelegate 使用它,那么使用它
  • 我在我的 viewcontroller .h 中添加了你的 .h 代码,在 .m 文件内的下载函数中添加了你的 .m 代码,我在哪里添加了委托方法?
  • 在 .m 中只需要粘贴我编写的委托方法
  • 删除没问题.. 它只是 MBProgressHUD
【解决方案2】:

如果您执行curl -vvv -o epub.pdf http://www.feedbooks.com/book/3471.epub,您将看到以下行:

Content-Length: 603244

content-length 标头是您正在下载的数据的字节大小。您可以在编写数据时使用它来跟踪您的进度。

使用您当前的代码,您无法真正做您想做的事。您应该查看this answer 了解更多信息。

【讨论】:

    【解决方案3】:

    您可以检查data NSData 的长度。然后你会发现实际下载的数据。

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    {
        // append the new data to the receivedData
    
        [receivedData appendData:data];
    }
    

    在这里您将获得以字节为单位的数据长度。您可以根据需要进行转换。

    也许对你有帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      相关资源
      最近更新 更多