Swift 4(Xcode 9.4) Alamo FireでMultipartFromDataを使用し画像送信したい
プログラミング初心者です。初めて質問させて頂きます。
Swift 4(Xcode 9.4) Alamo FireでMultipartFromDataを使用し画像送信したいのですが、
提携の構文がわからず、お教えて頂けましたら幸いです。
宜しくお願いいたします。
import Alamofire
public func uploadMultipartFromData(parameters: [[String: Any]]) {
let url = "http://127.0.0.1:8000/"
let urlRequest = URL(string: url)!
let headers: HTTPHeaders = [
"name": "value"
]
let parameters: Parameters = [
"str_param": "str value",
"num_param": 0,
"nested": [
"nested_param": "value"
]
]
Alamofire.request(url,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers)
.authenticate(user: "user", password: "password")
.responseJSON { response in
if
let json = response.result.value as? [String: Any],
let prop1 = json["prop1"] as? String
{
debugPrint(prop1)
}
debugPrint(response)
}
func appendMultipartFormData(multipartFormData: MultipartFormData, element: [String: Any]) {
switch element["value"] {
case let image as UIImage:
var imageData: Data
var mimeType: String
if element["extension"] as! String == "jpg" {
print(element["extension"] as! String)
imageData = UIImageJPEGRepresentation(image, 1.0)!
mimeType = "jpeg"
} else {
imageData = UIImagePNGRepresentation(image)!
mimeType = "png"
}
multipartFormData.append(
imageData,
withName: element["key"] as! String,
fileName: element["name"] as! String,
mimeType: mimeType
)
default:
let value = element["value"] as! String
multipartFormData.append((value.data(using: .utf8))!, withName: element["key"] as! String)
break
}
}
func uploaldMultipartProcess(upload: UploadRequest, completeNotificationName: NSNotification.Name) {
upload
.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { (response: DataResponse<Any>) in
let statusCode = response.response?.statusCode
print("Success: \(response.result.isSuccess)")
print("Status code: \(String(describing: statusCode))")
}
}