我正在努力使用Alamofire库将url下载到用户指定的路径

let savePanel = NSSavePanel()
        savePanel.allowedFileTypes = ["jpg","gif","png","webp"]
        let result = savePanel.runModal()
        if ( result == NSFileHandlingPanelCancelButton) {
            print("cancelled")
            return
        }

        if let fileUrl:NSURL = savePanel.URL {

            Alamofire.download(.GET, imageUrl, destination: fileUrl)

        }

上面的代码似乎给了我一个错误:
swift - alamofire Swift:无法将图片下载到用户指定的位置-LMLPHP
有人知道如何解决这个问题以帮助用户将图像下载到文件位置吗?

最佳答案

试试这一个它的工作为我:-

let manager = Alamofire.Manager.sharedInstance
manager.session.configuration.timeoutIntervalForRequest = 600

var localPath: NSURL?
manager.download(.GET, savePanel.URL,destination: { (temporaryURL, response) in
             //Create directory
             let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
             let myDirectory = documentsPath.URLByAppendingPathComponent("MyDirectory")
             do {
                  try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil)
                } catch let error as NSError {
                     NSLog("Unable to create directory \(error.debugDescription)")
               }
                let directoryURL = myDirectory
                let pathComponent = response.suggestedFilename

                localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
                return localPath!
            })
                .response { (request, response, _, error) in
                    print(response)
                    if error != nil {
                        print("REQUEST: \(request)")
                        print("RESPONSE: \(response)")
                    }
                    if localPath != nil {
                         print("from url Downloaded file to \(localPath!)")
                    }

            }

关于swift - alamofire Swift:无法将图片下载到用户指定的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38241300/

10-09 16:12