gpt4 book ai didi

ios - Swift 3 - Alamofilre 4.0 多部分图像上传有进展

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:22 25 4
gpt4 key购买 nike

我已经完成了图片上传,并使用以下方法成功上传到服务器。

现在我想上传图片及其进度,谁能告诉我该怎么做?我到处都找到了,但没有得到正确的解决方案。

没有进度的图片上传代码:

@IBAction func uploadClick(_ sender: AnyObject) {

// define parameters
let parameters = [
"file_name": "swift_file.jpeg"
]

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"http://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):

upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}

case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}

}

}

最佳答案

经过一番搜索,终于找到了解决方案。我们只需要将 uploadProgress block 放在结果 block 中。

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"http://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})

upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}

case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}

}

关于ios - Swift 3 - Alamofilre 4.0 多部分图像上传有进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631823/

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