无法使用来自源URL-swift的alamofire上传多个文件

问题描述 投票:1回答:1

我正在尝试使用Alamofire在ssme时上传图像文件(.jpg)和pdf文件(.pdf)。我一直收到以下错误,表明我尝试上传的文件不存在,实际上它们确实存在。

 multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “tempImage_wb.jpg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, NSFilePath=/var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, NSUnderlyingError=0x1c4841020 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}))

我上传文件的代码:

 // GET URL FOR IMAGE AND PDF FILES
    guard
        let imageURLString = UserDefaults.standard.string(forKey: "URL_IMAGE"),
        let pdfURLString = UserDefaults.standard.string(forKey: "URL_PDF") else{return}


    guard
        let imgURL = URL.init(string: imageURLString),
        let pdfURL = URL.init(string: pdfURLString) else{return}

    var arrayURLToUpload: [URL] = []
    arrayURLToUpload.append(imgURL)
    arrayURLToUpload.append(pdfURL)



 let sendParamters = ["user_id": "1", "hashTagArray": jsonArrayHashTags]
    Alamofire.upload(
        multipartFormData: { multipartFormData in
            for(key, value) in sendParamters{
                multipartFormData.append((value.data(using: .utf8)!), withName: key)
            }

            for fileURL in arrayURLToUpload{
                print("fileURL: \(fileURL)")
                multipartFormData.append(fileURL, withName: "file[]")
            }
    },
        to: UPLOAD_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }

                /**TRACK PROGRESS OF UPLOAD**/
                upload.uploadProgress { progress in
                    print(progress.fractionCompleted)


                }
                /***/


            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

安慰:

 fileURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg
 fileURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImagePDF.pdf

我知道文件存在是因为当我检索其中一个文件(.pdf)并显示它时我能够查看存储的pdf文件:

    guard let urlString = UserDefaults.standard.string(forKey: "URL_PDF") else{return}

    guard let pdfURL = URL.init(string: urlString) else{
        print("no pdf URL")
        return

    }
    print("pdfURL: \(pdfURL)")

    guard let pdf = PDFDocument.init(url: pdfURL) else{
        print("NO PDF DOCUMENT FOUND")
        return

    }

    pdfPreview.document = pdf // CAN VIEW PDF FILE!!!
    pdfPreview.autoScales = true

    print("pdf document displayed!")

安慰:

 pdfURL: file:///var/mobile/Containers/Data/Application/90ECD1AE-B9A5-46C9-AD30-C5D8D850A361/Documents/tempImagePDF.pdf

 pdf document displayed!

我如何生成我的网址:

 // Create a URL to save PDF
func createPdfURL() -> URL {

    let fileName = "tempImagePDF.pdf"

    let documentsDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentDirectory = documentsDirectories.first!
    let pdfPageURL = documentDirectory.appendingPathComponent("\(fileName)")

    return pdfPageURL
}

然后我保存PDF文档如下:

 // SAVE PDF TO LOCAL FILE SYSTEM
func savePdfToLocaFile(pdf: PDFDocument) -> Void {

    // CREATE URL FOR PDF DOCUMENT
    let pdfURL = createPdfURL()
    print("PDF SAVED TO URL: \(pdfURL)")

    self.pdfDocumentURL = pdfURL

    pdf.write(to: pdfURL)


}

我还尝试以下方法通过从文件名重建URL来检索文件,然后再使用它来上传文件:

 // CONSTRUCT URL FROM FILE NAME
    let imgFileName = "tempImage_wb.jpg"
    let pdfFileName = "tempImagePDF.pdf"

    var dir: URL!
    do {
        dir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    } catch{
        print("error re-creating url")
    }

    let imgURL = dir.appendingPathComponent(imgFileName)
    let pdfURL = dir.appendingPathComponent(pdfFileName)

实施@ vadian建议后的新控制台阅读:

PDF SAVED TO URL: file:///var/mobile/Containers/Data/Application/35C9D3EE-2D0C-4028-BCF3-2FE4581A0686/Documents/tempImagePDF.pdf
fileURL: file:///var/mobile/Containers/Data/Application/35C9D3EE-2D0C-4028-BCF3-2FE4581A0686/Documents/tempImagePDF.pdf
ios swift alamofire
1个回答
3
投票

请注意不同的应用程序容器标识符7FF74F06-CD6E-47D6-850E-45E768C00D9790ECD1AE-B9A5-46C9-AD30-C5D8D850A36

容器位置会定期更改,因此永远不会将指向应用程序容器的完整路径保存到UserDefaults。仅保存文件名,并始终使用URL获取当前文档目录的URL

let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

然后使用.appendingPathComponent附加文件名

注意:try!是安全的,因为文档目录始终由框架创建。

© www.soinside.com 2019 - 2024. All rights reserved.