如何使用 Alamofire (post) 上传图片?

Posted

技术标签:

【中文标题】如何使用 Alamofire (post) 上传图片?【英文标题】:How to upload pictures with Alamofire (post)? 【发布时间】:2019-03-29 12:21:21 【问题描述】:

我必须发出 API 请求以上传由相机拍摄或从图库中挑选的图像。 API中设置的方法已设置,在body/parameters中只有

key:file
value: // 

其中的值是图片本身。 (在邮递员中,我只是从我的文件中上传了一张图片,它就可以工作)它返回一个带有其他信息的 URL。 问题是我无法获得成功响应的请求,而且我不知道如何将 UIImage 传递给 Alamofire。 这就是我所做的

Alamofire.request(baseUrl,
          method: .post,
          parameters: ["file" : expenseImage])
.responseJSON(completionHandler:  response in
                            guard response.result.error == nil else 
                                print("Error subiendo la imagen \n\(response.result.error!)")
                                return
                            

                            guard let json = response.result.value as? [String: Any] else 
                                if let error = response.result.error 
                                    print("Error: \(error.localizedDescription)")
                                
                                return
                            
                            do 
                                let decoder = JSONDecoder()
                                let rawData = try JSONSerialization.data(withJSONObject: json, options: [])
                                let dataObject = try decoder.decode(PutObjectFile.self, from: rawData)
                                finished(dataObject)
                                print(dataObject)

                                print(dataObject.msg.file_info.url)
                             catch let error 
                                print("Error decoding:\n\(error)")
                            
                          )

我在控制台中遇到的错误如下:

responseSerializationFailed(reason: Alamofire.AFError
.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

我尝试使用随机图片网址,但它也不起作用

【问题讨论】:

【参考方案1】:

您可以使用多部分数据上传图片,使用以下代码:

let manager = Alamofire.SessionManager.default
    do
        manager.upload(multipartFormData:  (formData) in
            if let fileData = fileData  // File data
                formData.append(fileData, withName: "FILE_NAME", fileName: "simple.jpeg", mimeType: "image/jpeg")
            
        , to: "API_PATH", method: HTTPMethod.post, headers: nil, encodingCompletion:  encoding in
            switch encoding
            case .success(let req, _, _):
                req.uploadProgress(closure:  (prog) in
                    progress?(prog)
                ).responseJSON  (resObj) in
                    switch resObj.result
                    case .success:
                        if let resData = resObj.data
                            do 
                                let res = try JSONSerialization.jsonObject(with: resData)
                                print(res) // Success
                             catch let err
                                print(err)
                            
                        
                        break
                    case .failure(let err):
                        print(err)
                        break
                    
                
                break
            case .failure(let err):
                print(err)
                break
            
        )
    catch let err
        print(err)
    

【讨论】:

我收到此错误Cannot invoke 'append' with an argument list of type '((CGFloat) -> Data?, withName: String, fileName: String, mimeType: String)' 您需要将 UIImage 转换为 Data 以便附加到 formData 中。你做到了吗? 现在我做到了,我再次收到此错误responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) 【参考方案2】:

我遇到了同样的问题,我已经解决了这个问题: Alamofire 5.0.0-beta.5

AF.upload(multipartFormData:  multipartFormData in
        multipartFormData.append(imgData, withName: "file",fileName: "file.jpg", mimeType: "image/jpg")
            ,
        to: url).responseJSON
         response in print(response)
            if let status = response.response?.statusCode 
                                print(status)
                            
            //to get JSON return value
            if let result = response.value 
                let res = result as! [String: String]
                let newUrl:String = res["url"]!
                            print(newUrl)
            
        

【讨论】:

【参考方案3】:
 
        let id : String = UserDefaults.standard.string(forKey: "id")!
        print(id)

        self.view.makeToastActivity(.center)

        Alamofire.upload(multipartFormData:  (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(self.imageView.image!, 0.5)!, withName: "profile_pic", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in [“key”: "", ] 
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            
        , to:BaseURL + “base_url”)
         (result) in
            switch result 
            case .success(let upload, _, _):

                upload.uploadProgress(closure:  (Progress) in
                    print("Upload Progress: \(Progress.fractionCompleted)")
                )

                upload.responseJSON  response in

                    if let JSON = response.result.value as? NSDictionary
                    
                        print("JSON: \(JSON)")

                        let data = JSON["data"] as? NSArray


                        let status = JSON["status"] as? Int

                        if(status == 201)
                        


                        
                    

                    self.view.hideToastActivity()
                

            case .failure(let encodingError):
                //self.delegate?.showFailAlert()
                print(encodingError)
                self.view.hideToastActivity()
            

        
    

【讨论】:

【参考方案4】:

使用 Alamofire 的上传方法将您的图片上传到multipartFormData。我已经为你更新了代码。使用下面的代码。

Alamofire.upload(multipartFormData:  multipartFormData in
        multipartFormData.append(expenseImage, withName: "file",fileName: "file.jpg", mimeType: "image/jpg")
        ,
with: baseUrl)
 (result) in
    switch result 
    case .success(let upload, _, _):

        upload.uploadProgress(closure:  (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
        )

        upload.responseJSON  response in
             print(response.result.value)  
        

    case .failure(let encodingError):
        print(encodingError)  
    

【讨论】:

它也不起作用:( Expression type '()' is ambiguous without more context

以上是关于如何使用 Alamofire (post) 上传图片?的主要内容,如果未能解决你的问题,请参考以下文章

如何将Int值添加到Alamofire上传参数

如何记录 HTTP POST 请求

Alamofire的get请求,post请求,以及上传字典

使用 Alamofire 上传图像不成功

alamofire 上传图像数组不起作用

使用 Alamofire 上传图片以及其他字符串数据