如何在iOS 7中下载之前查找文件的大小?

问题描述 投票:4回答:7

想要在iOS 7中下载文件之前在某个服务器上找到文件的大小...我有一个NSURLConnectionDelegate的方法但在iOS 4.3之后它已被弃用

这是方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
iphone ios7 nsurlconnection
7个回答
2
投票

您应该使用NSURLConnectionDataDelegate注意:DATADelegate

和它的didReceiveResponse:方法发送HEAD request只获得标题:

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

然后你可以用@ Port的建议得到响应大小:

long long size = [response expectedContentLength];

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
投票

你可以使用这个方法,NSURLResponse对象传递给以下委托方法:

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

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

试试这样..

long long size = [response expectedContentLength];

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)")
}
}

0
投票

试试这会有所帮助。 。 。 。 。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
                                                                      *)response
{
    NSHTTPURLResponse * pHttpUrlResponse = (NSHTTPURLResponse *) response;

    if ([response respondsToSelector:@selector(allHeaderFields)])
    {
        NSString * fileLength = [NSString stringWithFormat:@"%lld",[pHttpUrlResponse expectedContentLength]];
    }    
}

快乐的编码:)


0
投票

同样使用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


-1
投票

试试这个,

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];

:)

© www.soinside.com 2019 - 2024. All rights reserved.