简体   繁体   English

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

[英]How to send image file via Alamofire?

I want to send an image to the server and I'm using this code: 我想将图像发送到服务器,并且正在使用以下代码:

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

but in response I get: 但作为回应,我得到了:

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

so my file is not sending to the server. 因此我的文件没有发送到服务器。 What is the wrong in my code? 我的代码有什么问题?

let imageData = UIPNGRepresentation(image)!

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

from: 从:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM