我需要從網址獲取文件大小以決定是否下載它。我嘗試了NSMutableURLRequest
,但在委託中,它始終返回-1。 NSMutableURLRequest
方法現在已被棄用。我在.h file
中添加了所有代表(NSURLConnectionDelegate
,NSURLConnectionDownloadDelegate
,NSURLConnectionDataDelegate
),但它不起作用。我的代碼是:How to find size of a file before downloading it in iOS 7?iOS 10在下載前獲取文件大小
適用於iOS 10+任何解決方案:
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://download.quranicaudio.com/quran/abdullaah_3awwaad_al-juhaynee/001.mp3"]];
[req setHTTPMethod:@"Head"];
NSURLConnection * con = [[NSURLConnection alloc] initWithRequest:req
delegate:self];
[con start];
[con release];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse (%lld) ", response.expectedContentLength);
}
與已經嘗試過?
爲什麼在剛剛開始下載時釋放'con'對象? – Torongo
我猜文件大小應該在url的元數據中 –
@Torongo,感謝您的期待,但我已經評論過該行。不工作了。答案是(-1) –