Al-*_*del 1 pdf ios swift alamofire
我想下载我使用此代码的 Pdf 文件 url:
class func downloadPdf(pdfReport: String, completion: @escaping (_ error: Error?, _ success: Bool,_ value: String) -> Void) {
let downloadUrl: String = URLs.pdfFileUrl + pdfReport
let destination = DownloadRequest.suggestedDownloadDestination()
print(downloadUrl, destination)
Alamofire.download(downloadUrl, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination).responseJSON { response in
switch response.result {
case .failure(let error):
print("error: ", error)
print(error.localizedDescription)
completion(error, false, "")
case .success(let value):
let json = JSON(value)
print(json)
print("successss")
completion(nil, true, "")
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在视图控制器中:
func downloadPdf() {
KRProgressHUD.show(withMessage: NSLocalizedString("wait", comment: "wait"))
API.downloadPdf(pdfReport: self.report.report){ (error: Error?, success: Bool, result: String) in
if success {
KRProgressHUD.dismiss()
} else {
KRProgressHUD.dismiss()
if (!Connectivity.isConnectedToNetwork()){
Toast.toast(messsage: NSLocalizedString("no internet", comment: "no internet"), view: self.view)
} else {
Toast.toast(messsage: NSLocalizedString("error occured", comment: "error occured"), view: self.view)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我downloadPdf第一次调用方法时,它成功下载了文件,但出现此错误:
https://madrasty.dev.ibtdi.work/public/reports/15287105135b1e4571f2596-new.pdf(函数)错误:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=384 around character) " UserInfo={NSDebugDescription=字符 0 周围的值无效。})) 由于错误,JSON 无法序列化:????? ????? ????????? ????? ?????????? ???????.
如果再次调用该方法,我会收到此错误:
错误:错误域=NSCocoaErrorDomain 代码=516 “?????? ??? “CFNetworkDownload_WBV95z.tmp” ??? “文档” ????? ????? ???? ?????? ???? ?。” 用户信息={NSSourceFilePathErrorKey=/Users/ibtdi/Library/Developer/CoreSimulator/Devices/10CB5B13-42C2-4646-934C-67FD33C948C5/data/Containers/Data/Application/F894940C-0D55-4950C-0D55-4950C-0D55-4950C55C55-4950C55C500000000000000000000000000000000000000000000000000 tmp, NSUserStringVariant=( 移动 ), NSDestinationFilePath=/Users/ibtdi/Library/Developer/CoreSimulator/Devices/10CB5B13-42C2-4646-934C-67FD33C948C5/data/Containers/Data/Application/F8905D48C5C5C4F89495C4C5C40C5C5C40C5 /Documents/15287105135b1e4571f2596-new.pdf, NSFilePath=/Users/ibtdi/Library/Developer/CoreSimulator/Devices/10CB5B13-42C2-4646-934C-67FD33C948C5/Data/Application/4F95C5/Data/Application/4F95C5C5/Data/4D95C5C5 E852C1C4B440/tmp/CFNetworkDownload_WBV95z。tmp, NSUnderlyingError=0x604000254460 {Error Domain=NSPOSIXErrorDomain Code=17 "文件存在"}} ???? ???“CFNetworkDownload_WBV95z.tmp”???“文件”????????? ???????????.
这个错误是因为在同一个目录中有一个同名的文件,我尝试使用responseString而不是,responseJSON但它没有解决我的问题,我该怎么办?
您的代码中几乎没有错误:
Data任务,而不是JSONVadian 指出的任务。将您的下载功能更改为:
func downloadPdf(pdfReport: String, uniqueName: String, completionHandler:@escaping(String, Bool)->()){
let downloadUrl: String = URLs.pdfFileUrl + pdfReport
let destinationPath: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("\(uniqueName).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
print(downloadUrl)
Alamofire.download(downloadUrl, to: destinationPath)
.downloadProgress { progress in
}
.responseData { response in
print("response: \(response)")
switch response.result{
case .success:
if response.destinationURL != nil, let filePath = response.destinationURL?.absoluteString {
completionHandler(filePath, true)
}
break
case .failure:
completionHandler("", false)
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的错误:
Error Domain=NSPOSIXErrorDomain Code=17 "File exists"
清楚地表明该文件路径中已经有一个文件。所以此代码将删除以前的文件(如果有)。
| 归档时间: |
|
| 查看次数: |
3810 次 |
| 最近记录: |