从远程 url 下载 urlSession 失败 - CFNetworkDownload_gn6wzc.tmp 出现 [英] urlSession download from remote url fail - CFNetworkDownload_gn6wzc.tmp appeared

查看:75
本文介绍了从远程 url 下载 urlSession 失败 - CFNetworkDownload_gn6wzc.tmp 出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to download file from remote url using urlSession. But when the download is completed an unexpected file appeared named CFNetworkDownload_gn6wzc.tmpthen i cannot open that file using uidocumentinteractioncontroller. What is that file? how to fix this issue. Thank for reply.

这里是留言

您的位置是 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

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

Here is my sample code: i send link through this method

@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) 方法中编写如下代码:

Downloaded data is stored in a temporary file. You need to write that data in your file and use that. Write following code in func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) method:

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 文件.

When any file is downloaded from server, your app keeps all the data in temporary file. After downloading the file it gives you the URL where it temporary keeps your data. Now it's up to you how and where you want to store that data. Temporary files are not stored in Document directory. You need to copy the content of that file in your file with correct extension. Replace the given method in your code. When you want to read that file use swift.pdf not the tmp one.

这篇关于从远程 url 下载 urlSession 失败 - CFNetworkDownload_gn6wzc.tmp 出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆