Reputation: 281
I have to upload pdf file as multi part form data.
I read Alamofire/Usage.md(Uploading Data to a Server)
So I write below code.
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
print(url) // file:///private/var/mobile/Containers/Data/Application/69C5B45A-AA29-46D2-909C-2A1A5A68C10F/tmp/com.test.test-Inbox/D5100_EN.pdf
do {
let data = try Data(contentsOf: url)
print(data) // 10899227 bytes
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "pdf")
}, to: "https://myurl.com")
.responseJSON { response in
debugPrint(response) // message = "Required request part 'file' is not present"
}
} catch {
print(error)
}
}
}
But it was filed.
How can I upload pdf file as multi part form data in alamofire?
Upvotes: 0
Views: 1690
Reputation: 3232
You need to specify the mimeType
:
multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: "pdf", mimeType:"application/pdf")
Updated
According to the error you must understand that server is expecting a pdf with name "file"
try this:
multipartFormData.append(pdfData, withName: "file", fileName: "file", mimeType:"application/pdf")
Upvotes: 2
Reputation: 3802
You probably need to specify MimeType
. Try updating your request to
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "name", fileName: "fileName", mimeType: "application/pdf")
}, to: "https://myurl.com")
.responseJSON { response in
debugPrint(response) // message = "Required request part 'file' is not present"
}
Upvotes: 1