gpt4 book ai didi

ios - 上传图片 Multipart With Moya Swift Fully 例子

转载 作者:搜寻专家 更新时间:2023-11-01 06:02:16 29 4
gpt4 key购买 nike

如何在带有 Moya 的 Swift 中使用 multipart/form-data 上传图像

我的代码

var multipartBody: [MultipartFormData]? {
switch self {
case .registerWith(let registrationObject ):
if let profileImageData = registrationObject.profileImageData{
return [MultipartFormData(provider: .data(profileImageData), name: "profile_picture", fileName: "\(registrationObject.phone).jpg",
mimeType: "image/jpeg")]
}
return nil
default:
return nil
}
}

var task: Task {
switch self {
case .registerWith:
if let multipartBody = multipartBody {
return .upload(.multipart(multipartBody))
}else{
return .request
}
default:
return .request
}
}

但我需要使用键“profileImage”在正文中发送图像数据

最佳答案

To upload an image in Multipart using Moya version 10.0.0 and above

1-将要上传的文件放在MultipartFormData数组中

// put files in MultipartFormData Arry  
var multipartBody :[Moya.MultipartFormData]?{
switch self {
case .registerWith(let imageData , let name ,let description ):
// any additional body data or body parms
let nameDataProvider = MultipartFormData(provider: .data(name.data(using: .utf8)!), name: "name")
let emailDataProvider = MultipartFormData(provider: .data(email.data(using: .utf8)!), name: "email")
// image want to upload it or file as a MultipartFormData
let imageDataProvider = Moya.MultipartFormData(provider: MultipartFormData.FormDataProvider.data(imageData), name: "avatar", fileName: "photo.jpg", mimeType: "image/jpeg")
return [ imageDataProvider , descriptionDataProvider ]
default:
return []
}
}

2-使该方法的任务类型为uploadMultipart

var task: Task {
switch self {
case .registerWith:
// for each method multi part use its multipartBody that declared it in step one needed
return .uploadMultipart(multipartBody!)
default:
guard let parameters = self.parameters else {
return .requestPlain
}
return .requestParameters(parameters: parameters.values, encoding: parameters.encoding)
}
}

To upload an image in Multipart using Moya version 9.0.0 and below

1-将要上传的文件放在MultipartFormData数组中

// put files in MultipartFormData Arry  
var multipartBody: [MultipartFormData]? {
switch self {
case .registerWith(let imageData , let name ,let description ):
return [MultipartFormData(provider: .data(imageData),
name: "profile_picture", fileName: "fileName", mimeType: "image/jpeg")]
default:
return nil
}
}

2- 将 multipartBody 的提供者添加到您需要的方法的请求体中

var parameters: [String: Any]? {
switch self {
case .registerWith(let registrationObject ):
var body = registrationObject.toJSON()!
// add the file in body
body["profile_picture"] = multipartBody?[0].provider
return body
}
}

3-使该方法的任务类型为上传

var task: Task {
switch self {
case .registerWith:
// multipartBody that in First step
return .upload(.multipart(multipartBody))
}
}

关于ios - 上传图片 Multipart With Moya Swift Fully 例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46185190/

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