下载时获取文件大小

简介:

下载时获取文件大小

获取文件大小,有时候会出现文件大小为0的情况,一般来说,这是由于我们无法获取文件头中的“Content-Length”造成,通常有两种原因:

 

1. 服务器压根就没有设置这个属性(现在的服务器基本上都有)

2. 由于将“Accept-Encoding“设置为gzip,就是压缩传输,从而将Content-Length属性隐藏了,导致我们无法获取

 

The expected content length is only set when the server provides it, such as by a Content-Length response header. A -1 size means the expected content size is unknown.

If you set Accept-Encoding: gzip on your request, the URL loading system will fib and tell you the expected size is -1, no matter what Content-Length the server sends. This is because it decompresses the data before passing it to you, but it can't know the final uncompressed size till all the data has been downloaded, which is well after you receive this callback.

 

针对第二种情况,首先可以检查响应头中的Accept-Encoding“的属性是否为"gzip",如果是的话,就在下载请求中手动设置Accept-Encoding“:

[req setValue:@""forHTTPHeaderField:@"Accept-Encoding"];

这时候再去检查响应头,Content-Length属性就出来了。

 

目录
相关文章
|
5月前
WordPress 修改上传文件大小限制
WordPress 修改上传文件大小限制
132 3
|
5月前
|
前端开发 JavaScript Java
文件在线压缩与解压系统
文件在线压缩与解压系统
|
10月前
文件上传 文件大小和类型
文件上传 文件大小和类型
|
10月前
|
Linux Docker 容器
查看服务器文件大小
查看服务器文件大小
98 0
|
Java 开发工具 UED
“文件的上传与下载:实现与优化“
“文件的上传与下载:实现与优化“
52 0
|
编解码 Java 应用服务中间件
文件的上传和下载
文件的上传和下载
88 0
|
Java 程序员
批量压缩16万个文件夹为压缩包(.zip格式)
🍅程序员小王的博客:程序员小王的博客 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕 🍅java自学的学习路线:java自学的学习路线
267 0
批量压缩16万个文件夹为压缩包(.zip格式)