我想在下载iOS 7上的文件之前找到服务器上文件的大小... 我有一个NSURLConnectionDelegate的方法,但这个方法在iOS 4.3之后就已经被弃用了。
以下是该方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
我想在下载iOS 7上的文件之前找到服务器上文件的大小... 我有一个NSURLConnectionDelegate的方法,但这个方法在iOS 4.3之后就已经被弃用了。
以下是该方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
使用HEAD方法发出请求。例如:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
这个请求与GET请求完全相同,但不会返回主体。然后调用
long long size = [response expectedContentLength];
使用 NSURLConnection 的完整示例(当然也适用于 NSURLSession):
NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil];
long long size = [response expectedContentLength];
NSLog(@"%lld",size);
如果服务器发送了Last-Modified标头,这也很有用,可以根据它进行有条件的下载。
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSString *lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
}
NSURLConnectionDataDelegate
而不是DATADelegate,然后使用它的didReceiveResponse:
方法发送HEAD请求以仅获取标头:- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
long long size = [response expectedContentLength];
如果您想使用 Swift,请按照以下步骤进行操作:
步骤1:使用 NSURLConnectionDataDelegate。
class ViewController: UIViewController,NSURLConnectionDataDelegate {
var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.twitter.com")!)
// Use **HEAD** request to get http header information.
request.HTTPMethod = "HEAD"
var connection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
{
var size = response.expectedContentLength
println("size : \(size)")
}
import UIKit
class ViewController: UIViewController,NSURLConnectionDataDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.twitter.com")!)
request.HTTPMethod = "HEAD"
var connection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
{
var size = response.expectedContentLength
println("size : \(size)")
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response {
}
long long size = [response expectedContentLength];
同时,使用URLSession
,您可以在不下载实际数据的情况下获取文件大小:
func getPdfSize(for url: URL) {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
URLSession.shared.dataTask(with: request) { [weak self] (_, response, _) in
if let response = response {
let size = response.expectedContentLength
DispatchQueue.main.async {
self?.fileSizeLabel.text = ByteCountFormatter.string(fromByteCount: size, countStyle: .file)
}
}
}.resume()
}
请注意,您还可以使用本机iOS ByteCountFormatter
将字节计数格式化为可读字符串。
Swift 5代码
试试这个,会有帮助的……
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
NSHTTPURLResponse * pHttpUrlResponse = (NSHTTPURLResponse *) response;
if ([response respondsToSelector:@selector(allHeaderFields)])
{
NSString * fileLength = [NSString stringWithFormat:@"%lld",[pHttpUrlResponse expectedContentLength]];
}
}
编程愉快 :)
试试这个,
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
:)