我想下载使用此代码的Pdf文件网址:
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, "")
}
}
}
并在视图控制器中:
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)
}
}
}
}
当我第一次调用downloadPdf
方法时,它成功下载了文件,但是我收到了这个错误:
https://madrasty.dev.ibtdi.work/public/reports/15287105135b1e4571f2596-new.pdf(功能) 错误:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效。”UserInfo = {NSDebugDescription =字符0周围的值无效}})) 由于错误,无法序列化JSON: تعذرتقراءةالبياناتنظرالأنتنسيقهاغيرصحيح。
如果再次调用该方法,我收到此错误:
错误:错误Domain = NSCocoaErrorDomain Code = 516“تعذرنقل”CFNetworkDownload_WBV95z.tmp“إلى”Documents“نظرالوجودعنصربنفسالاسم。”的UserInfo = {NSSourceFilePathErrorKey = /用户/ ibtdi /库/开发商/ CoreSimulator /设备/ 10CB5B13-42C2-4646-934C-67FD33C948C5 /数据/容器/数据/应用/ F894940C-0D55-4953-A9FA-E852C1C4B440 / TMP / CFNetworkDownload_WBV95z。 tmp,NSUserStringVariant =( 移动 ),NSDestinationFilePath = / Users / ibtdi / Library / Developer / CoreSimulator / Devices / 10CB5B13-42C2-4646-934C-67FD33C948C5 / data / Containers / Data / Application / F894940C-0D55-4953-A9FA-E852C1C4B440 / Documents / 15287105135b1e4571f2596-new .pdf,NSFilePath = / Users / ibtdi / Library / Developer / CoreSimulator / Devices / 10CB5B13-42C2-4646-934C-67FD33C948C5 / data / Containers / Data / Application / F894940C-0D55-4953-A9FA-E852C1C4B440 / tmp / CFNetworkDownload_WBV95z。 tmp,NSUnderlyingError = 0x604000254460 {错误域= NSPOSIXErrorDomain代码= 17“文件存在”}} تعذرنقل“CFNetworkDownload_WBV95z.tmp”إلى“Documents”نظرالوجودعنصربنفسالاسم。
此错误,因为同一目录中的文件名相同,
我尝试使用responseString
代替responseJSON
,但它无法解决我的问题,我该怎么办?
答案 0 :(得分:1)
您的代码中几乎没有错误:
Data
任务,而不是Vadian指出的JSON
任务。将下载功能更改为:
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
}
}
}
你错了:
Error Domain=NSPOSIXErrorDomain Code=17 "File exists"
清楚地告诉该文件路径上已存在文件。因此,如果存在任何文件,此代码将删除以前的文件。