Swift3 在Swift 3.0中设置Alamofire自定义目标文件名,而不是使用suggestedDownloadDestination

Swift3 在Swift 3.0中设置Alamofire自定义目标文件名,而不是使用suggestedDownloadDestination,swift3,alamofire,Swift3,Alamofire,如何在swift 3.0中编写以下代码段?以下语法在swift 2中 Alamofire.download(.POST, invoice.url,parameters:params, destination: { (url, response) -> NSURL in let pathComponent = response.suggestedFilename let fileManager = NSFileManager.defaultMana

如何在swift 3.0中编写以下代码段?以下语法在swift 2中

    Alamofire.download(.POST, invoice.url,parameters:params, destination: { (url, response) -> NSURL in

        let pathComponent = response.suggestedFilename

        let fileManager = NSFileManager.defaultManager()
        let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        let fileUrl = directoryURL.URLByAppendingPathComponent(pathComponent)
        return fileUrl
    })
    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
        print(totalBytesRead)
        dispatch_async(dispatch_get_main_queue()) {
            let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead)
            completionHandler(progress, nil)
        }
    }
    .responseString { response in
        print(response.result.error)
        completionHandler(nil, response.result.error)
    }

在Swift 3中是这样的

let parameters: Parameters = ["foo": "bar"]

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
    let pathComponent = "yourfileName"
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let fileURL = documentsURL.appendPathComponent(pathComponent)
    return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}

Alamofire.download(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)
    .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
        print("Progress: \(progress.fractionCompleted)")
    }
    .validate { request, response, temporaryURL, destinationURL in
        // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
        return .success
    }
    .responseJSON { response in
        debugPrint(response)
        print(response.temporaryURL)
        print(response.destinationURL)
    }

查看或以了解更多详细信息。

使用
func-appendingPathComponent(u.pathComponent:String)->URL
而不是
appendPathComponent

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
    let pathComponent = "yourfileName"
    let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
    let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
    return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
也可以使用
响应

let destination: DownloadRequest.DownloadFileDestination = { _, response in
    let pathComponent = response.suggestedFilename!
    let directoryURL: URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
    let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
    let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
    return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}

获取响应mime类型并添加正确的文件名扩展Alamofire版本是最新的4.2.0和swift 3.0,在我上面的代码中,它的let-pathComponent=response.suggestedFilename。如何编写此文件?@ZubinGala我没有尝试过,但是,在
下载请求中更改如下内容。下载文件目的地
用response替换第二个
,然后使用
让pathComponent=response。suggestedFilename
@ZubinGala欢迎伴侣:)