【问题标题】:How to uploading image with Alamofire and referencing the file in params如何使用 Alamofire 上传图像并在参数中引用文件
【发布时间】:2018-03-26 22:02:55
【问题描述】:

我可以将图像发送到 API(用于审核)并将其链接到邮递员中的“媒体”参数,如下所示:

现在我正试图在 swift 中做同样的事情。我按照示例 here 进行操作,但出现错误:未指定媒体。这是我的代码:附加的图像文件是否未正确链接到媒体参数?

    let image = self.descriptionImage.image!
    let parameters = [
        "api_user": "xxxxx",
        "api_secret": "xxxxx",
        "models": "nudity,wad",
        "media": "file.png"
    ]

    Alamofire.upload(multipartFormData: { multipartFormData in
        if let imageData = UIImageJPEGRepresentation(image, 1) {
            multipartFormData.append(imageData, withName: "file.png", fileName: "file.png", mimeType: "image/png")
        }

        for p in parameters {
            let value = p.value
            multipartFormData.append((value.data(using: .utf8))!, withName: p.key)
        }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.response { [weak self] response in
                        guard let strongSelf = self else {
                            return
                        }
                        print(response.data)
                        print("strongSelf")

                        debugPrint(response)

                    }
                case .failure(let encodingError):
                    print("error:\(encodingError)")
                }
    })

【问题讨论】:

    标签: swift http alamofire postman


    【解决方案1】:

    图片的参数应该在多部分表单数据中指定,尝试将代码更改为以下代码:

    let image = self.descriptionImage.image!
    let parameters = [
        "api_user": "xxxxx",
        "api_secret": "xxxxx",
        "models": "nudity,wad"
    ]
    
    Alamofire.upload(multipartFormData: { multipartFormData in
        if let imageData = UIImagePNGRepresentation(image) {
            multipartFormData.append(imageData, withName: "media", fileName: "file.png", mimeType: "image/png")
        }
    
        for p in parameters {
            let value = p.value
            multipartFormData.append((value.data(using: .utf8))!, withName: p.key)
        }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.response { [weak self] response in
                        guard let strongSelf = self else {
                            return
                        }
                        print(response.data)
                        print("strongSelf")
    
                        debugPrint(response)
    
                    }
                case .failure(let encodingError):
                    print("error:\(encodingError)")
                }
    })
    

    【讨论】:

    • omg 非常感谢你,我整晚都在努力解决这个问题:DDD
    • @user3517546 很高兴它对您有所帮助。
    猜你喜欢
    • 2014-12-17
    • 2017-08-15
    • 2017-07-25
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    相关资源
    最近更新 更多