想要在iOS 7中下载文件之前在某个服务器上查找文件的大小...我有一个NSURLConnectionDelegate方法,但在iOS 4.3之后不推荐使用
这是方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
答案 0 :(得分:11)
使用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"];
}
答案 1 :(得分:2)
您应该使用NSURLConnectionDataDelegate
注意:DATADelegate
及其didReceiveResponse:
方法发送HEAD request以获取标题:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
然后你可以通过@Port的建议获得响应大小:
long long size = [response expectedContentLength];
答案 2 :(得分:1)
你可以使用这个方法,NSURLResponse对象传递给下面的委托方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response {
}
试试这个..
long long size = [response expectedContentLength];
答案 3 :(得分:1)
对于 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)")
}
}
答案 4 :(得分:0)
试试这会有所帮助。 。 。 。 。
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
NSHTTPURLResponse * pHttpUrlResponse = (NSHTTPURLResponse *) response;
if ([response respondsToSelector:@selector(allHeaderFields)])
{
NSString * fileLength = [NSString stringWithFormat:@"%lld",[pHttpUrlResponse expectedContentLength]];
}
}
快乐的编码:)
答案 5 :(得分:0)
也可以使用data class Responseval(val `data`: List<Data>, val message: String, val success: Boolean)
data class Data(val created_at: String, val id: Int, val name: String, val status: Int, val updated_at: String)
来获得这样的文件大小,而无需下载实际数据:
URlSession
请注意,您还可以使用本机iOS 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()
}
将字节数格式化为可读的字符串。
Swift 5中的代码
答案 6 :(得分:-1)
试试这个,
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
:)