Swift Alamofire MultipartformData上传文件

时间:2018-11-13 21:48:19

标签: swift file-upload alamofire

我尝试使用alamofire multipartformdata,并尝试使用以下代码上传图像/参数:

static func fileUploads(url: String, parameters: [String: AnyObject], files: [Data], completion: @escaping (_ result: JSON)->()) {

        let headers: HTTPHeaders = [

            "Content-type": "multipart/form-data"

        ]

        Alamofire.upload(multipartFormData: { (multipartFormData) in

            for (key, value) in parameters {

                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)

            }

            for value in files {

                multipartFormData.append(value, withName: "images[]", fileName: "image.jpeg", mimeType: "image/jpeg")

            }

            multipartFormData.append("TEST".data(using: String.Encoding.utf8)!, withName: "test")

        }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("Succesfully uploaded")
                    print(response);
                    if let err = response.error{
                        //onError?(err)
                        return
                    }
                    //onCompletion?(nil)
                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")
                //onError?(error)
            }

        }

    }

我从服务器获得响应,但是如果我尝试获取服务器中的所有帖子数组,则会得到空数组。

0 个答案:

没有答案