xcode 下载前计算文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2558464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:15:09  来源:igfitidea点击:

Calculating File size before download

iphonexcodedownloadnsurlconnection

提问by Sagar R. Kothari

What I want to do is explained as follows.

我想要做的解释如下。

I have an url of MP3 file. ( for example Sound File)

我有一个 MP3 文件的网址。(例如声音文件

When user starts application, download should start & for that I have implemented following methods:

当用户启动应用程序时,下载应该开始,为此我已经实现了以下方法:

-(void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[NSURL URLWithString:@"http://xyz.pqr.com/abc.mp3"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];

    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    if(con){
        myWebData=[[NSMutableData data] retain];
        } else {
        //          [MainHandler performSelector:@selector(targetSelector:) withObject:nil];
      } 
     }


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"%@",@"connection established");
    [myWebData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@",@"connection receiving data");
    [myWebData appendData:data]; 
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@",@"connection failed");
    [connection release];
    //  [AlertViewHandler showAlertWithErrorMessage:@"Sorry, there is no network connection. Please check your network and try again."];
//  [self parserDidEndDocument:nil];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
}

Above methods work perfectly for downloading. But I can not get the exact size which is going to be downloaded. I want to know what is the size of file - which is going to be download.

以上方法非常适合下载。但我无法获得将要下载的确切大小。我想知道要下载的文件的大小。

回答by FKDev

Just use the expectedContentSize on the NSURLResponseobject which is passed to the following delegate methods:

只需在NSURLResponse传递给以下委托方法的对象上使用 expectedContentSize :

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}
-(NSURLRequest *)connection:(NSURLConnection *)connection
        willSendRequest:(NSURLRequest *)request
       redirectResponse:(NSURLResponse *)response {
}

like this:

像这样:

long long size = [response expectedContentLength];

回答by Sagar R. Kothari

Yep ! Almost done with ASIHTTP.

是的 !几乎完成了 ASIHTTP。

-(IBAction)btnTapped:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://sound20.mp3pk.com/indian/omkara/omkara1%28songs.pk%29.mp3"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:[self pathForLogInCCID]];
    [myProgressIndicator setProgress:0];

    [request setDownloadProgressDelegate:myProgressIndicator];
    [request startSynchronous];
[myProgressIndicator doubleValue]);

    [request setDelegate:self];
    [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
//  NSString *responseString = [request responseString];

    // Use when fetching binary data
//  NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
//  NSError *error = [request error];
}

-(NSString*)pathForLogInCCID{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"mySong.png"];
    return DataPath;
}