上传带有参数的图像(Alamofire)

问题描述 投票:0回答:2

我正在尝试使用 AlamoFire 将图像 (avatarImage) 作为 formData 参数类型上传到服务器,但每次尝试时都会生成“无法读取数据,因为它的格式不正确”错误邮政。我做错了什么?

class func createTeamWithAvatar(avatarImage: Image) {
    let extendedURI = "\(RequestManager.baseURL)" + "\(RequestManager.ClickUpURI.Team.rawValue)"
    RequestManager.sharedAlamofireManager.upload(multipartFormData: {
        multipartFormData in

        //This generates an error: "The data could not be read because it isn’t in the correct format"
        if let imageData = UIImageJPEGRepresentation(avatarImage, 1) {
            multipartFormData.append(imageData, withName: "avatar", mimeType: "image/jpeg")
        }
           
        }, to: extendedURI,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON {
                        response in
                        
                        if let statusCode = response.response?.statusCode,
                            let response = response.result.value as? Dictionary<String, AnyObject>,
                            let _ = response["id"] as? String
                            , statusCode == 200
                        {
                            completionHandler(response, nil)
                        } else {
                            let responseError = response.result.value as? Dictionary<String, AnyObject>
                            
                            let errorInfo = responseError ?? ["err" : "Unexpected media uploading error" as AnyObject]
enter code here
                            let error = response.result.error ?? RequestManager.makeError(response.response?.statusCode ?? 500, userInfo: errorInfo)
                            completionHandler(nil, error as NSError?)
                        }
                    }
                default:
                    completionHandler(nil, RequestManager.makeError(500, userInfo: ["err" : "Multipart encoding failed" as AnyObject]))
                }
        })
    }

PS:我使用 multipartFormData 上传,因为我还需要发送一些其他参数(一旦我能够上传图像)。

ios image alamofire
2个回答
0
投票

你应该使用

multipartFormData.append(paramData, withName: "paramName")

如果是字符串参数,您应该将字符串转换为数据

data = string.data(using: String.Encoding.utf8)!
multipartFormData.append(data, withName: "stringParamName")

完整代码

   RequestManager.sharedAlamofireManager.upload(multipartFormData: {
    multipartFormData in

    //This generates an error: "The data could not be read because it isn’t in the correct format"
    if let imageData = UIImageJPEGRepresentation(avatarImage, 1) {
        multipartFormData.append(imageData, withName: "avatar", mimeType: "image/jpeg")
    }
    data = string.data(using: String.Encoding.utf8)!
    multipartFormData.append(data, withName: "stringParamName")

   //..... 

0
投票

我必须添加 .httpAdditionalHeaders b/c 我的服务器需要某些数据:

RequestManager.sharedAlamofireManager.session.configuration.httpAdditionalHeaders = ["Accept": "application/json", "Content-Type": "multipart/form-data"]

您可以通过将键/值对附加到此数组来添加更多配置。

© www.soinside.com 2019 - 2024. All rights reserved.