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

时间:2018-07-12 12:39:40

标签: ios swift swiftydropbox

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

Dropbox file

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

    public string ExportPrices(bool _restorePrices = false)
    {
        System.IO.File.AppendAllText(System.Web.HttpContext.Current.Server.MapPath("~/Logs/PriceUpdateErrorList.txt"), "Error Messages" + Environment.NewLine);

        var db = new UniStockContext();
        //fetch all databases to export to
        IList<AccDatabase> accDBCol = db.AccDatabases.Where(x => (x.Active == true) && (x.Deleted == false)).ToList();
        //fetch all inventory list
        IList<Domain.Tables.Inventory> inventoryDBCol = db.Inventories.Where(x => (x.Active == true) && (x.Deleted == false)).ToList();

        if (inventoryDBCol.Count > 0)
        {
            //string xmlResult = "<InventoryExportResponse>";
            //loop through databases and export
            foreach (AccDatabase accDB in accDBCol)
            {
                //check database type and call appropriate accounting system method
                if ((accDB.Type == AccDatabaseType.SageEvolution) && string.IsNullOrEmpty(accDB.EvolutionCommon))
                {
                    //////////////////////////////////////////
                    //////////////Sage Evolution//////////////
                    //////////////////////////////////////////
                    foreach (AccDatabase accDB2 in accDBCol)
                    {
                        if ((accDB2.Type == AccDatabaseType.SageEvolution) && !string.IsNullOrEmpty(accDB2.EvolutionCommon))
                        {
                            //pass db and common db to export method for Sage Evolution
                            try
                            {
                                if (!string.IsNullOrEmpty(accDB.DBName))
                                {
                                    Thread thread = new Thread(()
                                        => inventoryDBCol = _sageInventory.ExportPrices(accDB, accDB2, inventoryDBCol, restorPrices: false)
                                        );
                                    thread.Start();

                                    //inventoryDBCol = _sageInventory.ExportPrices(accDB, accDB2, inventoryDBCol, restorPrices: false);//this call updates 13000 records
                                }
                            }
                            catch (Exception exExportPrices)
                            {
                                System.IO.File.AppendAllText(System.Web.HttpContext.Current.Server.MapPath("~/Logs/_sageInventoryExportPricesCallError.txt"), exExportPrices.ToString());
                            }
                            break;
                        }
                    }
                }
            }
            //end foreach

            //return (xmlResult + "</InventoryExportResponse>");
        }

        UpdatePricesFromInventoryListBulk(inventoryDBCol);//this call will update 40000 records in local DB.

        return "Stock prices synched to accounting system!";
    }

我尝试了不同的方法,但始终会遇到与“路径”相同的问题,错误始终是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) } } } 我尝试其他路径,但是是同样的问题。 你可以帮帮我吗?我的错误在哪里?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您要从API获取有关文件的元数据,则这将是FileMetadata对象的“ pathLower”属性。

client?.files.download(path: fileMetadata.pathLower!, overwrite: true, destination: destination)
                            .response { response, error in
                                if let response = response {
                                    print(response)
                                } else if let error = error {
                                    print(error)
                                }
                            }

答案 1 :(得分:0)

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

您可以使用listFolderlistFolderContinue来检索这些内容。

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

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