下载的PDF文件小且无法打开 - Swift iOS
Posted
技术标签:
【中文标题】下载的PDF文件小且无法打开 - Swift iOS【英文标题】:PDF file downloaded with small size and can't be opened - Swift iOS 【发布时间】:2021-12-30 12:05:50 【问题描述】:我正在尝试使用来自 API 的链接下载 pdf 文件,但它总是以 5 KB 的大小下载,并且文件总是损坏且无法打开,该链接工作正常,因为我已经尝试过了,我的代码:
import Foundation
import Alamofire
class DownloadOrderService
static let shared = DownloadOrderService()
func downloadPDFOrder(orderID: Int, completion: @escaping(_ error: Error?)-> Void)
let urlString = getJSON(strURL:"URL")
let destination: DownloadRequest.Destination = _, _ in
let documentsURL:NSURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
debugPrint("***documentURL: ",documentsURL)
let fileURL = documentsURL.appendingPathComponent("\(orderID).pdf")
debugPrint("***fileURL: ",fileURL ?? "")
return (fileURL!,[.removePreviousFile, .createIntermediateDirectories])
AF.download(urlString,method: .get, encoding: JSONEncoding.default, to: destination).downloadProgress(closure: (prog) in
).response response in
if response.error == nil
debugPrint(response)
completion(nil)
else
completion(response.error)
我在代码结果和响应中的所有打印语句:
"***documentURL: " file:///Users/mac/Library/Developer/CoreSimulator/Devices/71CB6514-5C19-423C-989C-B21F2CEFE65E/data/Containers/Data/Application/3F21E22E-469E-4BCD-8079-83BC4CE321F2/Documents/
"***fileURL: " file:///Users/mac/Library/Developer/CoreSimulator/Devices/71CB6514-5C19-423C-989C-B21F2CEFE65E/data/Containers/Data/Application/3F21E22E-469E-4BCD-8079-83BC4CE321F2/Documents/1007.pdf
[Request]: GET url
[Headers]: None
[Body]: None
[Response]:
[Status Code]: 200
[Headers]:
access-control-allow-headers: *
access-control-allow-methods: *
Access-Control-Allow-Origin: *
Cache-Control: no-cache, private
Content-Encoding: br
Content-Length: 1344
content-security-policy: upgrade-insecure-requests
Content-Type: text/html; charset=UTF-8
Date: Fri, 19 Nov 2021 20:25:54 GMT
Server: LiteSpeed
Set-Cookie: laravel_session=eyJpdiI6IjhPMFBvR2l6U2dwYk83OVIreWRqSUE9PSIsInZhbHVlIjoidU5yeEttN1BlQlRyXC9YVkh2d1RUYjJib3J1WkNtUUhVXC9oSVBzS08ybkhBYU9DT0tGaCtRbElBZzJiN2VRRFFvIiwibWFjIjoiYWQ2Y2UwZjhjZGI1MGUyYzg5MDNmOTA2YTVmNDI3MDZhM2JmZWM1NDFlOTRlZDY2NGFkNjk0MGQ5NTJjYTRjZSJ9; expires=Fri, 19-Nov-2021 22:25:54 GMT; Max-Age=7200; path=/; httponly; secure
Vary: Accept-Encoding
x-powered-by: php/7.4.11
[File URL]: /Users/mac/Library/Developer/CoreSimulator/Devices/71CB6514-5C19-423C-989C-B21F2CEFE65E/data/Containers/Data/Application/492B9611-A6BD-4456-BE3B-9E2419E11073/Documents/1007.pdf
[Resume Data]: None
[Network Duration]: 0.38273394107818604s
[Serialization Duration]: 0.0s
[Result]: success(Optional(file:///Users/mac/Library/Developer/CoreSimulator/Devices/71CB6514-5C19-423C-989C-B21F2CEFE65E/data/Containers/Data/Application/492B9611-A6BD-4456-BE3B-9E2419E11073/Documents/1007.pdf))
更新: 我什至创建了另一个函数,它再次导致我遇到同样的问题:
func savePdf(orderID: Int)
let urlString = getJSON(strURL:"url")
DispatchQueue.main.async
let pdfData = try? Data.init(contentsOf: urlString)
let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
let pdfNameFromUrl = "Orderatak-\(orderID).pdf"
let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl)
do
try pdfData?.write(to: actualPath, options: .atomic)
catch
debugPrint(error.localizedDescription)
【问题讨论】:
我在浏览器中试了链接,下载成功,大小为3MB 您现在可以检查问题的编辑,我已经包含了所有打印语句和响应 5k根本打不开 【参考方案1】:经过一些挖掘和尝试不同的链接后,我发现不幸的是,在与后端开发人员核实后,问题出在 URL 上,上面的两个代码都工作正常,您可以使用自己的复制和粘贴它们URL,不要忘记将以下属性添加到您的info.plist
并将它们设置为 YES 以便在您在真实设备中运行应用程序时能够找到下载的文件:
-
支持原地打开文档
应用程序支持 iTunes 文件共享
【讨论】:
以上是关于下载的PDF文件小且无法打开 - Swift iOS的主要内容,如果未能解决你的问题,请参考以下文章
无法在使用 Alamofire 下载的 PDFView IOS 中查看 PDF - SWIFT