想要在 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"];
}
您应该改用Note : DATADelegateNSURLConnectionDataDelegate
及其didReceiveResponse:
发送HEAD 请求以获取标头的方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
然后您可以通过@Port 的建议获得响应大小:
long long size = [response expectedContentLength];
对于Swift使用这些步骤,
第 1 步:使用NSURLConnectionDataDelegate。
class ViewController: UIViewController,NSURLConnectionDataDelegate {
第 2 步:创建连接请求。
var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.twitter.com")!)
// Use **HEAD** request to get http header information.
request.HTTPMethod = "HEAD"
第 3 步:创建连接。
var connection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
第 4 步:使用NSURLConnectionDataDelegate的委托方法
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)")
}
}
你可以使用这个方法,NSURLResponse 对象,它被传递给以下委托方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response {
}
试试这样。。
long long size = [response expectedContentLength];
试试这个会有所帮助。. . . .
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
NSHTTPURLResponse * pHttpUrlResponse = (NSHTTPURLResponse *) response;
if ([response respondsToSelector:@selector(allHeaderFields)])
{
NSString * fileLength = [NSString stringWithFormat:@"%lld",[pHttpUrlResponse 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()
}
还请注意,您可以使用本机 iOSByteCountFormatter
将字节数格式化为可读字符串。
Swift 5 中的代码
尝试这个,
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
:)