Module 'Alamofire' has no member named 'upload' - swift

I am trying to upload an image using swift to the server. i tried NSMutableURLRequest with URLSession. i received network connection lost. I thought it will be a better way to simply use Alamofire but got into a problem as xcode doesn't find the function update.
Any idea how to upload image with Alamofire? or find the update func?
the code for alamofire:
func uploadImageWithAlmofire(url: String) {
let params: Parameters = ["name": "abcd", "gender": "Male"]
Alamofire.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.yourimageView.image!, 0.1)!, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
for (key, value) in params
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:url,headers:nil)
{ (result) in
switch result {
case .success(let upload,_,_ ):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON
{ response in
//print response.result
if response.result.value != nil
{
let dict :NSDictionary = response.result.value! as! NSDictionary
let status = dict.value(forKey: "status")as! String
if status=="1"
{
print("DATA UPLOAD SUCCESSFULLY")
}
}
}
case .failure(let encodingError):
break
}
}
}

When you check Uploading Data to a Server example, it uses AF instead of Alamofire:
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "one")
multipartFormData.append(Data("two".utf8), withName: "two")
}, to: "https://httpbin.org/post")
.responseJSON { response in
debugPrint(response)
}

Try with this
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data(self.businessType.utf8), withName: "business_type")
let theFileName1 = (self.doc1.absoluteString as NSString).lastPathComponent
multipartFormData.append(self.doc1, withName: "key_value", fileName: theFileName1, mimeType: "image/png")
}, to: "https://www.test_document.php") //POST URL
.responseJSON { response in
debugPrint(response)
}

Related

Swift form-data using Alamofire 5 with parameters

I am beginner iOS developer and I don't know how to upload a file using Alamofire 5, I know there are a lot of similar questions, but all the topics here are from very old versions and I couldn't get it working. I tried this code, but I couldn't fit to my case, it gives me success, but file is not uploaded and the result I get is not what I get in postman. This is the code:
func uploadFile(_ parameters: Parameters) {
AF.upload(multipartFormData: { multipartFormData in
URLEncoding.default.queryParameters(parameters).forEach { (key, value) in
if let data = value.data(using: .utf8) {
multipartFormData.append(data, withName: key)
}
}
}, to: url)
.responseDecodable(of: FileCreation.self) { response in
switch response.result {
case .success(let data):
print(data, "success")
case .failure(let error):
print(error)
}
}
}
usage:
#IBAction func upload(_ sender: UIButton) {
guard let data = image?.jpegData(compressionQuality: 0.5)! else { return }
let parameters = ["addFiles": data]
uploadFile(parameters)
}
Here's Xcode output:
Here you can see postman response after I upload file:
Alamofire.upload(multipartFormData: {
multipartFormData in
if let imageData = image[0].jpegData(compressionQuality: 0.6) {
multipartFormData.append(imageData, withName: "addFiles", fileName: "file.pdf", mimeType: "application/pdf")
}
for (key, value) in param {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
},to: apiurl, method: .post, headers: headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON {
response in
print(response.result)
}
//break
case .failure(let encodingError):
break
}
})
Try This
func uploadFilesToServer(_ url: String, method: HTTPMethod, parameters: [String:Any]?, file: [String:Any]?, fileType: String, fileName: String, headers:HTTPHeaders?, completionHandler: #escaping (_ result: Data?, _ success: Bool, _ status: String) -> ()) {
var status = Bool()
var message = String()
let url = URL(string: url)
AF.upload(multipartFormData: { multiPart in
if let files = file {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy-hh-mm-ss"
let dateString = dateFormatter.string(from: Date())
for (key, value) in files {
if fileType == "pdf" {
let filePath = URL(fileURLWithPath: value as! String)
multiPart.append(filePath, withName: "\(key)", fileName: fileName, mimeType: "file/pdf")
} else {
multiPart.append(value as! Data, withName: "\(key)", fileName: "Uploads\(dateString).png", mimeType: "image/png")
}
}
}
if let params = parameters {
for (key, value) in params {
multiPart.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}
}, to: url!, method: method, headers: headers ?? nil)
.validate()
.uploadProgress(closure: { progress in
print(progress)
})
.responseJSON { response in
switch response.result {
case .success(let responseData):
print(responseData)
case .failure(let networkErr):
switch networkErr {
case .responseSerializationFailed(reason: _):
message = "Something went wrong"
case .sessionTaskFailed(error: let err):
message = err.localizedDescription
default:
message = "Something went wrong"
}
completionHandler(nil, false, message)
break
}
}
}
usage
uploadFilesToServer(url, method: .post, parameters: params, file: uploadFile, fileType: "pdf", fileName: fileNme, headers: tokenHeader) { [self] responseData, isSuccess, responseMsg in
if isSuccess {
} else {
}
}

Alamofire 5.4 upload progress issue

I am working on an app where I need to upload images and everything was working until I updated Alamofire to version 5.4.2 where I had a problem with upload progress.
The progress is showing 2 values.
func uploadImage(url: String, image: UIImage, params: [String: String], header: HTTPHeaders) {
AF.upload(multipartFormData: { (mp) in
if let imgData = image.jpegData(compressionQuality: 1.0) {
mp.append(imgData, withName: "image[0]", fileName: "\(UUID().uuidString).jpg", mimeType: "image/jpeg")
}
for (key, value) in params {
mp.append(value.data(using: .utf8)!, withName: key)
}
}, to: url, method: .post, headers: header)
.uploadProgress { (progress) in
let frac = progress.fractionCompleted
let percent = Int(frac * 100)
SVProgressHUD.showProgress(Float(frac), status: "\(percent)%")
print("\(percent)%")
}
.responseJSON { (response) in
SVProgressHUD.dismiss()
switch response.result {
case .success:
print("success !!")
case .failure(let error):
print(error.localizedDescription)
}
}
}
12%
100%
Is there any ideas how to fix this?

How to send UIImage Extension as parameter to server using Alamofire ppost request in Swift?

I have a postman api that requires an Extension to send with the image. Without the extension it take the image as a file. Help me to send the image with the extension or tell me the better way to do so, or let me know where i am doing wrong.Postman Image Here is my code so far:
func UPLOD(){
guard let token = UserDefaults.standard.string(forKey: "accesstoken") else {
return
}
print("Create button ACCESS KEY::::- \(token)")
let headers: HTTPHeaders = [
"x-access-token": token,
"Content-type": "multipart/form-data",
"Content-Disposition" : "form-data",
"Filename" : "Angle",
"Ext" : ".png"
]
let image = myImageView.image
let imgData = image!.jpegData(compressionQuality: 0.7)!
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imgData, withName: "FileToUpload", fileName: "swift_file.png", mimeType: "image/png")
print("mutlipart 1st \(multipartFormData)")
multipartFormData.append("angela".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"Filename")
multipartFormData.append("Image".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"Folder")
//multipartFormData.append("jpeg".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"Ext")
//multipartFormData.append(imgData, withName: "Ext", mimeType: "image/png")
}, to:"http://192.168.80.21:8800/api/v1/upload/uploadfile", method:.post, headers:headers)
{
(result) in
switch result {
case .success(let upload, _, _):
print("x:::::::::\(result)")
print("Upload;;\(upload)")
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress)")
})
upload.validate(statusCode: 200..<300)
upload.responseJSON { response in
guard response.error == nil else {
print("Pre json\(response as Any)")
let json = JSON(response)
print("JSON: \(json)")
return
}
print(response.result.value as Any)
print("Response: \(JSON(response.data as Any))")
}
case .failure(let encodingError):
print(encodingError)
}
}
}

How to work with Multipart using Alamofire in Swift with multiple images with different keys and parameters with multiple kinds

How to handle multipart in swift if you are dealing with multiple images having different keys and also having parameter dictionary with different kinds of key:value pairs like "String":"Any", "String":"[Any]", [[String:Any]].
let headers: HTTPHeaders = ["Content-type": "multipart/form-data"]
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
//Todo: - Images
var c = 0
for dictImage in arrImage {
let validDict = kSharedInstance.getDictionary(dictImage)
for keyName in validDict.keys {
print(keyName)
//Incr
c += 1
if let imageData = validDict[keyName] as? Data {
multipartFormData.append(imageData, withName: "\(keyName)", fileName: "\(Date().timeIntervalSince1970).jpeg", mimeType: "image/jpeg")
}
}
}
}, usingThreshold: UInt64(), to: urlString, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded")
if((response.result.value) != nil) {
debugPrint(response.result.value!)
let jsonData = JSON(response.result.value!)
if jsonData["status"].bool == true {
completion(jsonData.dictionaryObject!, true)
}else
{
completion(jsonData.dictionaryObject!, false)
}
}
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
failure(error, false)
}
}
Please check below code. Hope it helps you
Alamofire.upload(
multipartFormData: { MultipartFormData in
for (key, value) in parameters {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
for i in 0 ..< files.count {
let fileName : String = "image\(i).jpg"
let datum : Data = files[i]
MultipartFormData.append(datum, withName: fileParamName, fileName: fileName, mimeType: "image/jpg")
}
}, to: URLString, method: .post, headers: headers) { (result) in
switch(result) {
case .success(let upload, _, _):
upload.responseJSON { response in
}
break;
case .failure(_):
}
}

Alamofire 5 upload encodingCompletion

I'm working with Swift 4 and Alamofire 5, I upload two multibart photos and I want to print the progress
AF.upload(
multipartFormData: { MultipartFormData in
MultipartFormData.append(firstPic, withName: "first_pic", fileName: "image.jpeg", mimeType: "image/jpeg")
MultipartFormData.append(secondPic, withName: "second_pic", fileName: "image.jpeg", mimeType: "image/jpeg")
}, to: urlString, encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
}
upload.responseJSON { request, response, result in
print(result)
}
case .Failure(let encodingError):
print(encodingError)
}
})
and this gets an error saying
Argument labels '(multipartFormData:, to:, encodingCompletion:)' do not match any available overloads
did the library update the code or something??
Please modify according to your need
func upload(image: Data, to url: Alamofire.URLRequestConvertible, params: [String: Any]) {
AF.upload(multipartFormData: { multiPart in
for (key, value) in params {
if let temp = value as? String {
multiPart.append(temp.data(using: .utf8)!, withName: key)
}
if let temp = value as? Int {
multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
}
if let temp = value as? NSArray {
temp.forEach({ element in
let keyObj = key + "[]"
if let string = element as? String {
multiPart.append(string.data(using: .utf8)!, withName: keyObj)
} else
if let num = element as? Int {
let value = "\(num)"
multiPart.append(value.data(using: .utf8)!, withName: keyObj)
}
})
}
}
multiPart.append(image, withName: "file", fileName: "file.png", mimeType: "image/png")
}, with: url)
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of file
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON(completionHandler: { response in
//Do what ever you want to do with response
if let error = response.error {
print(error)
}
guard let data = response.value else {
return
}
print(value)
})
}
Alamofire 5 no longer requires an encodingCompletion! Instead, multipart form encoding is done as part of the standard now-asynchronous request process and will return errors on the Request, and they're available during validate and response* calls.
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "file.jpeg", mimeType: "image/jpeg")
},
to: "http://----/new.php", method: .post , headers: headers)
.response { resp in
print(resp)
}