Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift uploading PDF file with Alamofire (Multipart)

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

like image 248
Napsters Desmars Avatar asked Jan 09 '17 22:01

Napsters Desmars


1 Answers

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.

like image 198
Napsters Desmars Avatar answered Nov 08 '22 12:11

Napsters Desmars