gpt4 book ai didi

ios - 通过 Alamofire 上传图片失败

转载 作者:行者123 更新时间:2023-11-29 00:03:54 26 4
gpt4 key购买 nike

enter image description here

我正在上传多部分表单数据中的图像,但没有成功并抛出错误。但我在 postman 中尝试它成功了。我不知道我在哪里犯了错误。我附上了 postman 截图以上传响应。请检查它,我正在使用 Almofire 进行响应。

 func uploadimages() {

let url = ServiceUrl.Base + "ShopRegistration/ShopPicture"
print("URL === > \(url)")
print(self.imgData?.count)
var token :String = ""
if let strToken = Preference.GetString(key: UserDefaultsKey.Token) {
token = strToken
}
var RequestDist : NSDictionary = NSDictionary()
RequestDist = ["ShopId": "\(Preference.GetInteger(key: UserDefaultsKey.ShopID))"]
as NSDictionary;
print(RequestDist)
if(Reachability.isConnectedToNetwork())
{
Alamofire.upload(multipartFormData: { (multipartFormData) in
if self.imgData != nil && (self.imgData?.count)! > 0 {
for dataImg in (self.imgData)! {
//shopImage
multipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.jpeg", mimeType: "image/jpeg")
}
}
for (key, value) in RequestDist {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key as! String )
}
print("Request ===>>> /n \(multipartFormData.contentType)")
}, to:url,headers :["authToken" : token])
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("\n")
print(Progress.fractionCompleted)
})
upload.responseJSON { response in
if(response.result.isSuccess){
print("\n\n")
print("\(response.result.isSuccess)")
print("\n\n")
print(response.result)
print("\n\n")
print(response)
print("\n\n")
appDelegate.window?.rootViewController?.view.makeToast(message: "Images added sucessfully")
let datastring = NSString(data:response.data!, encoding:String.Encoding.utf8.rawValue) as String?
print("Response:::>>>> \(String(describing: datastring))")


if let intShopID : Int = Preference.GetInteger(key: UserDefaultsKey.ShopID) {
self.getShopImagesCall(intshopID: intShopID)
}

}else{
appDelegate.window?.rootViewController?.view.makeToast(message: AppMessage.getErrorInResponse)
}
}
case .failure(let encodingError):
appDelegate.window?.rootViewController?.view.makeToast(message: AppMessage.getErrorInResponse)
break
}
}
}
}

最佳答案

用于网络上使用的 JPEG 图像let imageData = UIImageJPEGRepresentation(img, 0.5) 并在正文处multipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.jpeg", mimeType: "image/jpeg")

对于 PNG 图像let image = UIImagePNGRepresentation(pickedImage) 和 bodymultipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.png", mimeType: "image/png").

关于ios - 通过 Alamofire 上传图片失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com