消息关闭
    暂无新消息!
swift3  Alamofire4.4 多参数,从相册里面选取图片,上传的方法怎么写,求大神。被憋了很久了。。。

1个回答

︿ 2
   //图片上传
    class func requestUpload(url: String, params: [String: String], data: [Data], success: @escaping(_ response: [String: AnyObject])->(), fail:@escaping(_ error: Error) -> ()){
        
        let headers = ["content-type":"multipart/form-data"]
        
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            //多张图片上传
//            let flag = params["flag"]
//            let userId = params["userId"]
//            
//            multipartFormData.append((flag?.data(using: String.Encoding.utf8))!, withName: "flag")
//            multipartFormData.append((userId?.data(using: String.Encoding.utf8))!, withName: "userId")
            
            for i in 0..<data.count{
                //设置图片的名字
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyyMMddHHmmss"
                let string = formatter.string(from: Date())
                let filename = "\(string).jpg"
                multipartFormData.append(data[i], withName: "img", fileName: filename, mimeType: "image/jpeg")
            }
        }, to: url, headers: headers, encodingCompletion:{ encodingResult in
            switch encodingResult{
            case .success(request: let upload,_,_):
                upload.responseJSON(completionHandler: { (response) in
                    if let value = response.result.value as? [String : AnyObject]{
                        success(value)
                    }
                })
            case .failure(let error):
                fail(error)
            }
        })
    }