使用NSURLSession下载时获取文件路径 [英] get file path when downloading with NSURLSession
问题描述
我想在下载时播放音乐,所以我要播放带有本地文件的音乐,如何在通过NSURLSession下载时在tmp目录中获取文件路径。
I want to play music while downloading, so i play music with local file, how can i get file path in tmp directory while downloading with NSURLSession.
方法 URLSession:downloadTask:didFinishDownloadingToURL:只能在文件下载时调用,所以我该怎么办
The method URLSession:downloadTask:didFinishDownloadingToURL: than only call while file downloaded, so what should i do
推荐答案
如果要在下载文件时获取文件位置,请不要使用下载任务。例如,创建一个 NSURLSessionDataTask
并将其自己流式传输到持久性存储(例如,为持久性本地文件创建自己的 NSOutputStream
存储并打开它,然后在 didReceiveData
中,将数据写入流中,并在完成后关闭流)。然后,您可以对流/文件执行其他任何操作。如果使用AFNetworking,我相信 AFHTTPRequestOperation
也会为此公开一个 outputStream
属性。
If you want to get file location as it's being downloaded, just don't use download task. For example, create a NSURLSessionDataTask
and stream it to persistent storage yourself (e.g. create your own NSOutputStream
for local file in persistent storage and open it, and then in didReceiveData
, just write the data to your stream, and close the stream when you're all done). Then you can do whatever else you want with the stream/file. If using AFNetworking, I believe that the AFHTTPRequestOperation
exposes an outputStream
property for this purpose, too.
这篇关于使用NSURLSession下载时获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!