gpt4 book ai didi

swift - urlSession 从远程 url 下载失败 - 出现 CFNetworkDownload_gn6wzc.tmp

转载 作者:行者123 更新时间:2023-11-30 11:29:37 24 4
gpt4 key购买 nike

我试图使用urlSession从远程URL下载文件。但是,当下载完成后,出现了一个名为 CFNetworkDownload_gn6wzc.tmp 的意外文件,然后我无法使用 uidocumentinteractioncontroller 打开该文件。 该文件是什么?如何解决这个问题。谢谢回复。

这里是消息

您的位置是 file:///Users/pisal/Library/Developer/CoreSimulator/Devices/26089E37-D4EA-49E5-8D45-BB7D9C52087D/data/Containers/Data/Application/767E966E-5954-41BA- B003-90E2D27C5558/Library/Caches/com.apple.nsurlsessiond/Downloads/com.SamboVisal.downloadTask/CFNetworkDownload_gn6wzc.tmp

这是我的示例代码:我通过此方法发送链接

@IBAction func startDownload(_ sender: Any) {
let url = URL(string: "http://www.tutorialspoint.com/swift/swift_tutorial.pdf")!
let task = DownloadManager.shared.activate().downloadTask(with: url)
task.resume()

}

下载完成后:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("Download finished: \(location)")
debugPrint("download task : \(downloadTask)")
ViewController().documentInteraction(location: location)

}

当我尝试打开uidocumentinteractioncontroller

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let destinationPath = documentDirectoryPath.appendingPathComponent("swift.pdf")
let destinationUrl = URL(fileURLWithPath: destinationPath)

do{
try FileManager.default.copyItem(at: locat, to: destinationUrl)
}
catch {
print("error copying file: \(error.localizedDescription)")
}

documentInteractionController.url = destinationUrl

if !documentInteractionController.presentOpenInMenu(from: openButton.bounds, in: view, animated: true) {
print("You don't have an app installed that can handle pdf files.")
}

最佳答案

下载的数据存储在临时文件中。您需要将该数据写入文件并使用它。在 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) 方法中编写以下代码:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("Download finished: \(location)")
do {
let downloadedData = try Data(contentsOf: location)

DispatchQueue.main.async(execute: {
print("transfer completion OK!")

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
let destinationPath = documentDirectoryPath.appendingPathComponent("swift.pdf")

let pdfFileURL = URL(fileURLWithPath: destinationPath)
FileManager.default.createFile(atPath: pdfFileURL.path,
contents: downloadedData,
attributes: nil)

if FileManager.default.fileExists(atPath: pdfFileURL.path) {
print("pdfFileURL present!") // Confirm that the file is here!
}
})
} catch {
print(error.localizedDescription)
}
}

编辑

从服务器下载任何文件时,您的应用程序会将所有数据保存在临时文件中。下载文件后,它会向您提供临时保存数据的 URL。现在由您决定如何以及在何处存储这些数据。临时文件不存储在 Document 目录中。您需要将该文件的内容复制到您的文件中,并使用正确的扩展名。替换代码中给定的方法。当您想阅读该文件时,请使用 swift.pdf 而不是 tmp 文件。

关于swift - urlSession 从远程 url 下载失败 - 出现 CFNetworkDownload_gn6wzc.tmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50383343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com