gpt4 book ai didi

iphone - 下载前计算文件大小

转载 作者:行者123 更新时间:2023-12-05 09:25:30 25 4
gpt4 key购买 nike

我想做的事情解释如下。

我有一个 MP3 文件的 url。 (例如 Sound File)

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

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

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

最佳答案

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

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

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

像这样:

long long size = [response expectedContentLength];

关于iphone - 下载前计算文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2558464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com