iOS Swift uploading PDF file with Alamofire (Multipart)

只谈情不闲聊 提交于 2020-03-13 04:21:10

问题


I'm currently developing an application using iOS 10 and Swift 3 and Alamofire 4

The purpose of this application is to upload a PDF file generated previously.

The PDF generation is working perfectly and the file is created.

However the upload doesn’t work…
 I received a success response but the file is not uploaded.

My server response

Multi part Content-Type => multipart/form-data; boundary=alamofire.boundary.56958be35bdb49cb
Multi part Content-Length => 293107
Multi part Content-Boundary => alamofire.boundary.56958be35bdb49cb
 responses 
SUCCESS: {
    uploadedFiles =     (
                {
            details = " Key=Content-Disposition - values=[form-data; name=\"pdfDocuments\"] length=8";
            storedFileName = "/var/www/pdf/17/009/22/TMP104150531290406.tmp";
            type = PDF;
            uploadedDate = 1483999296701;
            uploadedFileName = UnknownFile;
        }
    );
}
end responses

I’m using multi-part to upload my file as Data as you can see here

File url is fine.

I have searched on SO but didn’t find any solution working…

Here you can see my Controller

Alamofire.upload(
            multipartFormData: {
                multipartFormData in

                if let urlString = urlBase2 {
                    let pdfData = try! Data(contentsOf: urlString.asURL())
                    var data : Data = pdfData

                    multipartFormData.append(data as Data, withName:"test.pdf", mimeType:"application/pdf")
                    for (key, value) in body {
                        multipartFormData.append(((value as? String)?.data(using: .utf8))!, withName: key)
                    }

                    print("Multi part Content -Type")
                    print(multipartFormData.contentType)
                    print("Multi part FIN ")
                    print("Multi part Content-Length")
                    print(multipartFormData.contentLength)
                    print("Multi part Content-Boundary")
                    print(multipartFormData.boundary)
                }
        },
            to: url,
            method: .post,
            headers: header,
            encodingCompletion: { encodingResult in

                switch encodingResult {

                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print(" responses ")
                        print(response)
                        print("end responses")

                        onCompletion(true, "Something bad happen...", 200)

                    }
                case .failure(let encodingError):
                    print(encodingError)
                    onCompletion(false, "Something bad happen...", 200)
                }
        })

Thanks in advance for the help.

Regards


回答1:


I have just found my solution to fix this bug.

I have forgot a parameter for the file name.

multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: namePDF, mimeType:"application/pdf")

Thanks for the help.



来源:https://stackoverflow.com/questions/41557938/ios-swift-uploading-pdf-file-with-alamofire-multipart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!