gpt4 book ai didi

ios - 如何通过Alamofire发送图像文件?

转载 作者:行者123 更新时间:2023-11-30 13:50:56 27 4
gpt4 key购买 nike

我想将图像发送到服务器,并且我使用以下代码:

Alamofire.upload(
.POST,
"https://api.mysite.com/image",
multipartFormData: { multipartFormData in
if let _ = image {
if let imageData = UIImageJPEGRepresentation(image!, 1.0) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.png", mimeType: "image/png")
}
}
}, encodingCompletion: {
encodingResult in

switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .Success:
debugPrint(response)
case .Failure(let error):
print(error)
}

}
case .Failure(let encodingError):
print(encodingError)
}
}
)

但我得到的回应是:

[Result]: SUCCESS: (
"",
{
file = {
error = 1;
name = "file.png";
size = 0;
"tmp_name" = "";
type = "";
};

所以我的文件没有发送到服务器。我的代码有什么问题?

最佳答案

let imageData = UIPNGRepresentation(image)!

Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}

来自:

https://github.com/Alamofire/Alamofire#uploading-data

关于ios - 如何通过Alamofire发送图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286567/

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