gpt4 book ai didi

ios - 如何使用此数据分段上传

转载 作者:行者123 更新时间:2023-11-30 12:08:12 29 4
gpt4 key购买 nike

我想以多部分的形式将此数据与图像/视频一起发送到服务器。我有字节数据形式的图像/视频数据(NSData 或 Data)

{"mediauploadata":
{
"type":"poll",
"server_token":"03f0e635b4c01b9f398de393259de8650b54c85c24f49998af50593643f559230d95e8e605612653769f4871b543e25d48bf",
"id":"105"
}
}

最佳答案

我使用 Alamofire 创建了一个用于分段上传的函数。

通过传递 Web 服务 URL、参数和您的图像来调用此函数。

func WSPostAPIMultiPart(_ aStrULR:String,
param: [String: Any],
image : UIImage?,
controller: UIViewController,
successBlock: @escaping (_ response:[String : Any]?) -> Void,
failureBlock: @escaping (_ error: Error?) -> Void) {

var dictHeaders : HTTPHeaders = [String : String]()

if (Alamofire.NetworkReachabilityManager()?.isReachable)!{
Alamofire.upload(
multipartFormData: { MultipartFormData in

let JSONData: Data? = try? JSONSerialization.data(withJSONObject: param, options: .prettyPrinted)
MultipartFormData.append(JSONData!, withName: "json")
if image != nil{
MultipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: "profile_image",fileName: "image.jpeg", mimeType: "image/jpeg")
// withName : You will pass the key name required by your server
}

}, to: aStrULR ,method : .post , headers : dictHeaders) { (result) in

switch result {
case .success(let upload, _, _):

upload.responseJSON { response in

if let value = response.result.value {
let dictResponse = JSON(value).dictionaryObject

successBlock(dictResponse)
}
}

case .failure(let error):
failureBlock(error)
}
}
}else{
print("NO INTERNET CONNECTIVITY")
}
}

对于视频:只需向 videoURL 传递一个额外参数并附加为:

MultipartFormData.append(videoUrl, withName: "profileVideo", fileName: "video.mp4", mimeType: "video/mp4")

希望有帮助:)

关于ios - 如何使用此数据分段上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423878/

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