イメージをDbに更新しようとすると大きな問題が発生しました。Multipart Image Alamofire 4 swift 3
イメージといくつかの文字列値をアップロードする必要があります。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageCropped = selectedImage
self.sendImageWithMultipart()
imagePicker.dismiss(animated: true, completion: { _ in })
}
}
func sendImageWithMultipart() {
Alamofire.upload(multipartFormData: {
multipartFormData in
multipartFormData.append("\(HGYGjihf746fg743g)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"auth_token")
multipartFormData.append("\(0)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os")
multipartFormData.append("\(10.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os_version")
multipartFormData.append("\(3.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"app_version")
if let photo = self.imageCropped, let jpegImage = UIImageJPEGRepresentation(photo, 50.0) {
print(jpegImage)
multipartFormData.append(jpegImage, withName: "profile_photo", mimeType: "image/*")
}
}, to: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)", method:.post, headers:["Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__"], encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.validate().responseJSON {
response in
if response.result.isFailure {
debugPrint(response)
} else {
debugPrint(response)
}
}
case .failure(let encodingError):
NSLog((encodingError as NSError).localizedDescription)
}
})
}
が、結果はこれです:問題は、私は画像、またはコンテンツタイプまたは何を変換する方法である
[Request]: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)"
[Response]: <NSHTTPURLResponse: 0x17023c0e0> { URL: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)" } { status code: 500, headers {
"Cache-Control" = "no-cache, private";
Connection = close;
"Content-Length" = 5545;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 10 Nov 2016 10:49:00 GMT";
Server = "Apache/2.4.18 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.21";
"X-Powered-By" = "PHP/5.6.21";
} }
[Data]: 5545 bytes
[Result]: FAILURE: responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))
[Timeline]: Timeline: { "Request Start Time": 500467740.667, "Initial Response Time": 500467740.793, "Request Completed Time": 500467743.733, "Serialization Completed Time": 500467743.733, "Latency": 0.126 secs, "Request Duration": 3.066 secs, "Serialization Duration": 0.000 secs, "Total Duration": 3.066 secs
?
これは潜在的にあなたの問題を解決する可能性があり、あなたに
MIMEタイプを明示的に指定するにはどうすればいいですか? 'multipartFormData.append(jpegImage、withName:" profile_photo.jpeg "、mimeType:" image/jpeg ") ' – Enix
あなたは解決できましたか? – Cmag
私はこの問題を解決できませんでした。そして、私もmimetypeを指定しようとしましたが、何もありません。 –