如何使用iPhone SDK获取远程文件的大小?
我的 iPhone 应用程序从互联网下载大文件(例如视频)。
我想显示一个进度条,所以我需要获取我尝试下载的远程文件的大小(当然,在下载之前)。
我一次又一次地寻找,但没有任何效果。 有什么办法可以做到这一点吗?
预先感谢您。
My iPhone application downloads large files from the internet (videos, for instance).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 URL 加载系统(NSURLRequest、NSURLConnection 等),请查看 NSURLConnection 的预期内容长度是否有值。 请注意,它可能是 NSURLResponseUnknownLength。
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.
好吧,我终于得到答案了。
NSURLConnection 调用其委托
,我们需要查看 NSURLResponse 内部,以找到预期的ContentLength
Ok, I finally get my answer.
The NSURLConnection calls its delegate with
Well, we need to look inside the NSURLResponse, to find the expectedContentLength
您需要某种机制让服务器向您报告大小。
我希望大多数服务器在响应中提供内容长度标头条目。 这应该告诉你文件有多大。 我假设您用来下载的任何类都会向您公开该信息。
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.