เอกสาร PDF ดาวน์โหลดแฟ้มกับขนาดเล็กและไม่สามารถถูกเปิดใช้งานก็ต่อเมื่อ-รีบ iOS

0

คำถาม

ฉันพยายามที่จะดาวน์โหลดเป็นเอกสาร pdf องแฟ้มใช้การเชื่อมโยงจากรูปแบบ api แต่มันเป็นเรื่องดาวน์โหลดกับขนาด 5 กิโลไบต์และอยู่เสมองแฟ้มเสียหายและไม่สามารถเปิดใช้งานการเชื่อมต่ทำงานดีอย่างที่ฉันพยายามมันของฉันรหัส:

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)
           }
       }
   }
alamofire downloadfile ios pdf
2021-11-19 18:43:32
1

คำตอบที่ดีที่สุด

0

หลังจากลองหาข้อมูลและพยายามต่างออกที่อยู่เชื่อมโยงฉันได้ค้นพบว่าโชคไม่ดีที่หลังเช็คกับที่ปรับแต่งโปรแกรมเบื้องหลั dev,ประเด็นออกจากที่อยู่ URL และทั้งสองคนอื่นอยู่เหนือกำลังทำงานไม่เป็นไรคุณสามารถคัดลอกและวางพวกเขาใช้ที่อยู่ URL ของตัวเองและอย่าลืมเพิ่มที่ด้านล่างแอททริบิวต์ต่างๆของคุณ info.plist และตั้งพวกเขาต้อง ใช่ เป็นต้องหาดาวน์โหลดแฟ้มถ้าคุณกำลังวิ่งอแอ๊ปอยู่จริงของอุปกรณ์:

  1. สนับสนุนการเปิดเอกสารในสถานที่
  2. โปรแกรมสนับสนุนไอจูนส์การใช้งานแฟ้มร่วมกัน
2021-11-21 16:26:11

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่