Alamofire Multipart image upload failed
You are sending wrong filename in request.
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(d1, withName: "club_image",fileName: "file.jpg", mimeType: "image/jpg")// d1 in let d1 = UIImageJPEGRepresentation(uiimage, 1)
},
to:"mysite/upload.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
Uploading an image with Alamofire not succeeding
You are sending ID parameter along with the Image. So try to use utf8 encoding.
multipartFormData.append(operaID.data(using: .utf8)!, withName: "id")
I'm trying to upload an array of images using Alamofire but getting error
You need to convert the image to data
imagesArray.indices.forEach {
multipartFormData.append(imagesArray[$0].jpegData(compressionQuality:0.8)!, withName: "photo[\($0)]", fileName: "photo\($0).jpeg", mimeType: "image/jpeg")
}
Alamofire multipart upload post error in Swift
I had the same problem , as Michal said it's an installation issue if you are using cocoapods
go to your project files/pods/Alamofires/Source
and make sure you have 9 files there just like this image and make sure that the MultipartFormData.swift
file is there
if you are not using cocoapods
just open the Alamofire
folder and go to source folder and make sure you have the same files there too
if you didn't find MultipartFormData.swift
that means that your Alamofire version does not support MultipartFormData then you will need to get the latest version from here Alamofire or if you use cocoapods
you will have to update the line of Alamofire
pod
on podfile
to be like this
platform :ios, '8.0'
use_frameworks!
target 'ProjectName' do
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'master'
end
target 'ProjectNameTests' do
end
after updating your pod file you need to run this command to update the cocoapods project
pod install --verbose
Alamofire Multipart request
Multipart Request using almofire
Data send into multipart
struct AGImageInfo {
var fileName: String
var type: String
var data: Data
}
Request
let header: HTTPHeaders = [
"Content-Type":"application/x-www-form-urlencoded"
]
let parameters: Parameters = [
"someParam": "value",
"Image": AGImageInfo(fileName: "nameOfImage.jpeg", type: "image/jpeg", data: #imageLiteral(resourceName: "TO").toData()!)
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
if let imageData = value as? AGImageInfo {
multipartFormData.append(imageData.data, withName: key, fileName: imageData.fileName, mimeType: imageData.type)
}
multipartFormData.append(((value as AnyObject).data(using: String.Encoding.utf8.rawValue))!, withName: key)
}
}, to: "URL", method: .post, headers: header) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .success(let value):
debugPrint(value)
break
case .failure(let error):
debugPrint(error)
break
}
}
case .failure(let error):
debugPrint(error)
break
}
}
Swift Alamofire multipart form data URLRequest has no member failure
The Alamofire 5.0 migration document says this:
MultipartFormData
’s API has changed and the top level upload methods to create and upload MultipartFormData have been updated to match other request APIs, so it’s not longer necessary to deal with the Result of the multipart encoding.
(https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%205.0%20Migration%20Guide.md)
The code that you're using looks to be from an earlier version. In 5.0, the trailing closure uses a URLRequest
as its parameter (explaining the errors that you're seeing).
The Usage guide (https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md) provides this as an example of uploading multipart form data:
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
}, to: "https://httpbin.org/post")
.responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
(HTTPBinResponse
is an example of a Decodable
that you can replace with your own).
Besides responseDecodable
, it looks like you can also use responseJSON
, responseData
, responseString
etc.
Note that all of these are appended with a .
after the closing )
of the call -- not as a trailing closure.
Related Topics
How to Pass a Swift Object to JavaScript (Wkwebview/Swift)
Why Swift Use a Struct as Dictionary Key Instead of a String Here
How to Handle Usernotifications Actions in iOS 10
How to Properly Handle a Nil Uiapplication.Sharedapplication().Keywindow
Calculate New Coordinates with Starting Position and Distance
Operation Went Isfinished=Yes Without Being Started by the Queue It Is In
Swift Bridging Header File Won't Work with Use_Frameworks
Swift: Obtain and Save the Updated Scnnode Over Time Using Projectpoint in Scenekit
How to Update Particular Value of Child in Firebase Db
Swift: Sort Array by Sort Descriptors
Drag Uibutton Without It Shifting to Center [Swift 3]
How to Open Amazon App from Within My App
Custom Table View Row Action (Image)
How to Use Alamofires Servertrustpolicy.Disableevaluation in Swift 3
Exception: Cannot Manually Set the Delegate on a Uinavigationbar Managed by a Controller
Xcode 9.3 - Nspredicate Bool Crash
Swift Parse - Local Datastore and Displaying Objects in a Tableview