下载时获取文件大小

下载时获取文件大小

获取文件大小,有时候会出现文件大小为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属性就出来了。

 

 

转载于:https://www.cnblogs.com/YouXianMing/p/3788400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值