【问题标题】:Download And Save File From URL in Swift [duplicate]在 Swift 中从 URL 下载并保存文件 [重复]
【发布时间】:2023-03-03 05:02:23
【问题描述】:

我正在努力...

  • 从服务器下载文件(MLModel)

  • 将该文件保存到设备

  • 重新启动应用程序

  • 使用保存文件的 URL(路径)来访问下载的文件。所以我不必每次应用启动时都重新下载

让下载 = URLSession.shared.downloadTask(with: url) { (urlOrNil, response, error) in

    if let error = error {
                print("❌ There was an error in \(#function) \(error)")
                completion(nil)
                return
            }


// the location the mlModel is saved at
            guard let fileURL = urlOrNil else {print("????❇️>>>\(#file) \(#line)"); return  }
            do {
                //compiles the model
                let compiledUrl = try MLModel.compileModel(at: fileURL)

            //turns the model into an MLModel
            let model = try MLModel(contentsOf: compiledUrl)
            print("newModel Complete ????")


            // find the app support directory
            let fileManager = FileManager.default
            // Finds a place to save the MLModel
            let appSupportDirectory = try! fileManager.url(for: .applicationSupportDirectory,
                                                           in: .userDomainMask, appropriateFor: compiledUrl, create: true)

            // create a permanent URL in the app support directory
            let permanentUrl = appSupportDirectory.appendingPathComponent(compiledUrl.lastPathComponent)



            print("????\(permanentUrl)")


            // if the file exists, replace it. Otherwise, copy the file to the destination.
            if fileManager.fileExists(atPath: permanentUrl.absoluteString) {
                _ = try fileManager.replaceItemAt(permanentUrl, withItemAt: compiledUrl)
            } else {
                try fileManager.copyItem(at: compiledUrl, to: permanentUrl)
            }

            let newModel = try MLModel(contentsOf: permanentUrl)
            print("????", newModel)



            completion(newModel)
        }catch{
            print("❌ There was an error in \(#function) \(error) : \(error.localizedDescription)")
        }
    }.resume()

据我了解,这是将我的 MLModel 保存到“permanentUrl”。所以我重新启动应用程序,我不再运行 downloadTask(因为应该保存 MLModel?),我尝试使用此代码访问我复制到 PermanentURL 的 MLModel

let permanentUrl = URL(string:"file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application%20Support/CFNetworkDownload_v3EpLD.mlmodelc")

    do {
        let model = try MLModel(contentsOf: permanentUrl!)

        print(model)
    }catch{
        print("❌ There was an error in \(#function) \(error) : \(error.localizedDescription)")
    }

我收到此错误

file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application 귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc 不存在" UserInfo={NSLocalizedDescription=file:// /var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application 귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc 不存在}:file:///var/mobile/Containers/Data/Application/ 659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/库/应用程序귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc不存在

所以我猜 MLModel 没有保存?我在这里想念什么?我假设我用 downloadTask 保存了 MLModel,我只是没有以正确的方式检索它。如何访问我保存的文件?

【问题讨论】:

  • 我看到很多文章看起来有些相似,但在经过多次尝试和迭代后,答案对我不起作用。我可能只是不够聪明/经验不足,无法弥补不同的变量。如果我应该这样做,我深表歉意。

标签: swift coreml urlsession mlmodel


【解决方案1】:

这就是答案。

在 downloadTask 函数中,我打印了compiledUrl.lastPathComponent 并保存了它。然后,当我重新运行应用程序时,我运行此代码来查找文件的 URL。

// saved compiledUrl.lastPathComponent
    let filename = "CFNetworkDownload_B9QvT1.mlmodelc"


    let fileManager = FileManager.default
    let appSupportDirectory = try! fileManager.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

    let permanentUrl = appSupportDirectory.appendingPathComponent(filename)

    let model = try? MLModel(contentsOf: permanentUrl)
    print(model)

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 1970-01-01
    • 2019-11-18
    • 2011-01-31
    • 1970-01-01
    • 2016-05-30
    • 2019-12-16
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多