简体   繁体   English

使用Alamofire下载文件

[英]File download with Alamofire

I'm downloading a file to a custom file path like this: 我正在将文件下载到这样的自定义文件路径:

let tempFilePath = self.makeTempFilePath()
Alamofire.download(.GET, downloadURL) { temporaryURL, response in
    return tempFilePath
}

How do I know when the download is complete and the success/failure status? 我如何知道下载何时完成以及成功/失败状态? I tried responseData closure but result parameter always appears as "failure". 我尝试了responseData关闭,但result参数始终显示为“失败”。

I'm afraid that you can do is use the Downloading a File w/Progress explained in the documentation of Alamofire like the following: 恐怕您只能使用Alamofire文档中说明的“ 下载带有进度的文件”,如下所示:

Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
     .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
         print(totalBytesRead)

         // This closure is NOT called on the main queue for performance
         // reasons. To update your ui, dispatch to the main queue.
         dispatch_async(dispatch_get_main_queue) {
             print("Total bytes read on main queue: \(totalBytesRead)")
         }
     }
     .responseData { response in
         print(response)
     }

With the above code you can know the totalBytesExpectedToRead and the totalBytesRead and in part know the difference between both to check if the file was downloaded or not. 通过上面的代码,您可以知道totalBytesExpectedToReadtotalBytesRead并且可以部分了解两者之间的区别,以检查文件是否已下载。

In any case you can use too the Accessing Resume Data for Failed Downloads explained in the Alamofire doc in any case of failure during the download process. 在任何情况下,如果在下载过程中发生任何失败,您都可以使用Alamofire文档中说明的访问失败下载的继续数据

I hope this help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM