如何使用iPhone SDK获取远程文件的大小?

时间:2020-12-30 10:43:00

My iPhone application downloads large files from the internet (videos, for instance).

我的iPhone应用程序从互联网上下载大文件(例如视频)。

I'd like to display a progress bar, so I need to get the size of the remote file I'm trying to download (before downloading it, of course).

我想显示一个进度条,所以我需要获取我正在尝试下载的远程文件的大小(当然,在下载之前)。

I've searched, again and again, but nothing is working. Is there any way to do that ?

我一次又一次地搜索,但没有任何工作。有没有办法做到这一点?

Thank you by advance.

提前谢谢你。

3 个解决方案

#1


If you're using the URL Loading System (NSURLRequest, NSURLConnection, etc.), then look to see if there's a value for the NSURLConnection's expectedContentLength. Note that it might be NSURLResponseUnknownLength.

如果您正在使用URL加载系统(NSURLRequest,NSURLConnection等),那么请查看NSURLConnection的expectedContentLength是否有值。请注意,它可能是NSURLResponseUnknownLength。

#2


Ok, I finally get my answer.

好的,我终于得到了答案。

The NSURLConnection calls its delegate with

NSURLConnection调用其委托

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

Well, we need to look inside the NSURLResponse, to find the expectedContentLength

好吧,我们需要查看NSURLResponse,找到expectedContentLength

- (long long)expectedContentLength

Return Value The receiver’s expected content length, or NSURLResponseUnknownLength if the length can’t be determined.

返回值接收方的预期内容长度,如果无法确定长度,则为NSURLResponseUnknownLength。

#3


You'll need some mechanism for the server to report the size to you.

您需要一些机制让服务器向您报告大小。

I would expect most servers to provide the content-length header entry in the response. That should tell you how big the file is. I would assume whatever class you're using to do the download would expose that information to you.

我希望大多数服务器在响应中提供内容长度的头条目。这应该告诉你文件有多大。我会假设你用来做下载的任何课程都会向你公开这些信息。

#1


If you're using the URL Loading System (NSURLRequest, NSURLConnection, etc.), then look to see if there's a value for the NSURLConnection's expectedContentLength. Note that it might be NSURLResponseUnknownLength.

如果您正在使用URL加载系统(NSURLRequest,NSURLConnection等),那么请查看NSURLConnection的expectedContentLength是否有值。请注意,它可能是NSURLResponseUnknownLength。

#2


Ok, I finally get my answer.

好的,我终于得到了答案。

The NSURLConnection calls its delegate with

NSURLConnection调用其委托

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

Well, we need to look inside the NSURLResponse, to find the expectedContentLength

好吧,我们需要查看NSURLResponse,找到expectedContentLength

- (long long)expectedContentLength

Return Value The receiver’s expected content length, or NSURLResponseUnknownLength if the length can’t be determined.

返回值接收方的预期内容长度,如果无法确定长度,则为NSURLResponseUnknownLength。

#3


You'll need some mechanism for the server to report the size to you.

您需要一些机制让服务器向您报告大小。

I would expect most servers to provide the content-length header entry in the response. That should tell you how big the file is. I would assume whatever class you're using to do the download would expose that information to you.

我希望大多数服务器在响应中提供内容长度的头条目。这应该告诉你文件有多大。我会假设你用来做下载的任何课程都会向你公开这些信息。