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)!
}
本文介绍了一种解决阿里云OSS表单直传中因Content-Type与Content-Disposition顺序不当导致的照片上传失败问题的方法。通过重写MultipartFormData.swift中的encodeHeaders方法,确保Content-Disposition始终位于Content-Type之前,从而提高上传成功率。
1562

被折叠的 条评论
为什么被折叠?



