I am new in swift and I am not able to upload any file. I am able to upload only Image file
my code is like this
// import Alamofire
func uploadWithAlamofire() {
let image = UIImage(named: "bodrum")!
// define parameters
let parameters = [
"hometown": "Pune",
"living": "Pune",
"bill":PDFfile,
]
Alamofire.upload(multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png")
}
for (key, value) in parameters {
multipartFormData.append((value?.data(using: .utf8))!, withName: key)
}}, to: "upload_url", method: .post, headers: nil,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { [weak self] response in
guard let strongSelf = self else {
return
}
debugPrint(response)
}
case .failure(let encodingError):
print("error:\(encodingError)")
}
})
}
I want to upload any format not just image in PDFfile.
You just need to add the format whatever you want to add like this :
Pdf :
multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: namePDF, mimeType:"application/pdf")
xls :
let filePath = direcoryUrl.path+"/app.xls"
var bytes = [UInt8]()
if let data = NSData(contentsOfFile: filePath) {
var buffer = [UInt8](repeating: 0, count: data.length)
data.getBytes(&buffer, length: data.length)
bytes = buffer
}
Alamofire.upload(multipartFormData: {
multipartFormData in
multipartFormData.append(Data(fromArray: bytes), withName: "app",fileName: "app.xls", mimeType: "application/octet-stream")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.