ios - Swift - Alamofire totalBytesExpectedToRead 返回 -1,即使设置了 Content-length

标签 ios swift alamofire

即使 Content-length 有值,totalBytesExpectedToRead 也返回 -1

我找到了相同的 problem在 stackoverflow 上,但没有得到答复

这是我的代码:

Alamofire.download(.POST, urlString, headers: headers, destination: destination)   
       .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            print("totalBytesRead: \(totalBytesRead)")
            print("totalBytesExpectedToRead: \(totalBytesExpectedToRead)\n\n")
        }
        .response { request, response, data, error in
        }

服务器响应:

Access-Control-Allow-Origin →*
Access-Control-Expose-Headers →Link
Connection →keep-alive
Content-Length →163
Content-Type →application/json; charset=utf-8
Date →Tue, 04 Oct 2016 07:12:56 GMT

结果:

totalBytesRead: 1211
totalBytesExpectedToRead: -1

最佳答案

服务器发送给您的内容肯定有问题:公布的 Content-Length(163 字节)小于您实际收到的内容(1211 字节)。

似乎只有一次对进度 block 的调用,我会说 Alamofire 将 totalBytesExpectedToRead 设置为 -1 作为对服务器宣布的内容与实际发布的内容之间不一致的响应收到。

关于ios - Swift - Alamofire totalBytesExpectedToRead 返回 -1,即使设置了 Content-length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847042/

相关文章:

使用 PayPal 移动 SDK 的 iOS 构建错误

ios - 使用扩展/UIgraphicscontext 将表情符号(字符串)呈现为 iOS 中可能的最高分辨率图像

swift - 当应用程序处于后台时,Alamofire 后台上传不会打印进度

reference - swift3 中的模糊引用

ios - SpriteKit SKLightNode 衰减属性没有效果

json - 如何在 Swift 中使用 JSON 发出 Alamofire 请求后转换 AnyObject 中的 <AnyObject> 响应?

ios - 推送到另一个 View 时查看顶部更改并再次弹出

ios - 不能在 swift 中划分两个 NSNumbers?

swift - 转到下一个 ViewController 并在条件有效时传递数据

ios - 如何使用 Swift 清除 SearchBar 结果并在 tableview 中显示完整数据?