如何使用SwiftyDropbox下载文件?路径错误

问题描述 投票:0回答:1

我正在尝试使用SwiftyDropbox下载文件,但我对路径有疑问。我在mi Dropbox“prueba.txt”中有一个文件:

Dropbox file

这是我用来在我的应用程序中下载的代码。

import UIKit
import SwiftyDropbox

let clientDB = DropboxClientsManager.authorizedClient

class Controller: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: {
        (url: URL) -> Void in UIApplication.shared.open(url)
    })

    let fileManager = FileManager.default
    let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let destURL = directoryURL.appendingPathComponent("/test.txt")
    let destination: (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
        return destURL
    }

    clientDB?.files.download(path: "/prueba.txt", overwrite: true, destination: destination)
        .response{ response, error in
            if response != nil{
                self.cargarDatosCliente()
                //print (response)
            } else if let error = error{
                print (error)
            }
        }

        .progress{ progressData in
            print(progressData)
        }
    }
}

我尝试不同的方式但总是获得与“路径”相同的问题,总是错误是path/not_found/...我尝试与其他路径,但是同样的问题。你可以帮帮我吗?我的错误在哪里?

谢谢!

ios swift swiftydropbox
1个回答
0
投票

问题是“/prueba.txt”是本地文件路径。 Dropbox希望您为其远程服务器提供文件路径。

您可以使用listFolderlistFolderContinue检索它们。

例如,如果要检索应用程序或dropbox的根文件夹中的文件路径,请使用:

var path = ""
clientDB?.files.listFolder(path: path).response(completionHandler: { response, error in
            if let response = response {
                let fileMetadata = response.entries
                if response.hasMore {
                    // Store results found so far
                    // If there are more entries, you can use `listFolderContinue` to retrieve the rest.
                } else {
                    // You have all information. You can use it to download files.
                }
            } else if let error = error {
                // Handle errors
            }
        })

fileMetadata包含您需要的路径。例如,您可以像这样获取第一个文件的路径:

let path = fileMetadata[0].pathDisplay
© www.soinside.com 2019 - 2024. All rights reserved.