【发布时间】:2015-05-23 10:41:50
【问题描述】:
我有一个问题,我想在开始下载之前获取我放在文本字段中的链接的文件大小并在标签中显示它。
我做了几件事,我和[operation.response expectedContentLength]
我只得到 0。我在 .m 文件中的代码是这样的:
NSString *fileName = [self.linkTextBox.stringValue lastPathComponent];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.linkTextBox stringValue]]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSLog(@"size :%lld", [operation.response expectedContentLength]);
self.detailText.stringValue = [NSString stringWithFormat:@"%lld", [operation.response expectedContentLength]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"bytesRead: %lu, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", (unsigned long)bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
【问题讨论】:
-
请 webdeveloper 给你尺寸...如果不下载,你无法获得尺寸
-
预期长度可能为 0,因为服务器没有为您提供 Content-Length 标头。检查 HTTP 响应以查看它是否可用。
-
我在开始下载时获得了总长度,但在开始之前它不可用。我的链接长度显示在其他下载管理器中,但不是我的
标签: objective-c cocoa afnetworking nsurl