Swift 3 - Alamofilre 4.0 多部分图像上传进度 [英] Swift 3 - Alamofilre 4.0 multipart image upload with progress

查看:29
本文介绍了Swift 3 - Alamofilre 4.0 多部分图像上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've done with image upload and its successfully uploaded on server using below method.

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

Now I want to upload image with its progress so can any one tell me how to do? I found on everywhere but didn't got the correct solution.

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

@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块.

Finally got the solution after search a lot. We just need to put uploadProgress block within result 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)
        }

    }

这篇关于Swift 3 - Alamofilre 4.0 多部分图像上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆