【问题标题】:Swift 3 - Alamofilre 4.0 multipart image upload with progressSwift 3 - Alamofilre 4.0 分段图像上传进度
【发布时间】:2017-01-30 14:31:45
【问题描述】:

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

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

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

@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)
        }

    }

}

【问题讨论】:

    标签: ios swift alamofire swift3


    【解决方案1】:

    经过大量搜索终于得到了解决方案。我们只需要将 uploadProgress 块放在结果块中。

    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)
            }
    
        }
    

    【讨论】:

    • 非常感谢,我一直在寻找答案,终于可以在服务器上看到我的图像了
    猜你喜欢
    • 2017-01-28
    • 2016-06-02
    • 2017-04-28
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多