oss表单直传,The body of your POST request is not well-formed multipart/form-data 原文
"Content-Type: image/jpeg\r\n
Content-Disposition: form-data; name=\"file\"; filename=\"1158626664830205952.jpg\"\r\n\r\n"
50%概率Content-Type在Content-Disposition之前,从而导致上传照片失败
解决方案:重写这个方法就可以,只要让Content-Disposition一直放在第一位
Alamofire: 类名 MultipartFormData.swift
// private func encodeHeaders(for bodyPart: BodyPart) -> Data {
// var headerText = ""
//
// for (key, value) in bodyPart.headers {
// headerText += "\(key): \(value)\(EncodingCharacters.crlf)"
// }
// headerText += EncodingCharacters.crlf
//
// return headerText.data(using: String.Encoding.utf8, allowLossyConversion: false)!
// }
private func encodeHeaders(for bodyPart: BodyPart) -> Data {
//第一步:字典转有序数组
var headerArr = [String]()
for (key, value) in bodyPart.headers {
let subHeaderText = "\(key): \(value)\(EncodingCharacters.crlf)"
if key == "Content-Disposition" {
//添加到数组的第一个位置
headerArr.insert(subHeaderText, at: 0)
}else{
headerArr.append(subHeaderText)
}
}
//第二步:拼接数组内容
var headerText = ""
for text in headerArr {
headerText += text
}
headerText += EncodingCharacters.crlf
return headerText.data(using: String.Encoding.utf8, allowLossyConversion: false)!
}