OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 swift 4 从数据中使用 Alamofire 多部分发送参数数组 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:22
标题: ios - 如何使用 swift 4 从数据中使用 Alamofire 多部分发送参数数组

我正在使用 Alamofire 将图像和文件上传到服务器。但是我在发送带有图像的参数数组时遇到问题。但是当我在 params 中发送一个数组时,它会将数组转换为 JSON 字符串。但我想在参数中发送一个数组,而不是在 JSON 字符串中。我进行了很多搜索,但没有得到任何解决方案。这是下面的代码。

let params = ["id":"101","arrayParam":["1232","12344","14325"]]

    let url = www.khxjjhdfsj.com/hsdgs
            let headers: HTTPHeaders = [
                /* "Authorization": "your_access_token",  in case you need authorization header */
                "Content-type": "multipart/form-data"
            ]
            Alamofire.upload(multipartFormData: { (multipartFormData) in
                for (key, value) in params
                {
                     multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                }
                if let data = imageData
                {
                    multipartFormData.append(data, withName: "file", fileName: fileName, mimeType: "image/png")
                }
                if let data = pdfData
                {
                    multipartFormData.append(data, withName: "file", fileName: fileName, mimeType:"application/pdf")
                }
            }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
                switch result{
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print("Succesfully uploaded")
                        if let err = response.error
                        {
                            onError?(err)

                            return
                        }



                    }
                case .failure(let error):
                    print("Error in upload: \(error.localizedDescription)")
                    onError?(error)
                   }
            }



Best Answer-推荐答案


 let params = [
        "key":value,
        "key1": key2] as [String : Any]



    let manager = Alamofire.SessionManager.default
    manager.session.configuration.timeoutIntervalForRequest = 30000

    manager.request(url, method: .post, parameters: params)
        .responseJSON {


            response in

            switch response.result {
            case .success:
               stopActivityIndicator()
                print(response)
                if let result = response.result.value {
                    let JSON = result as! NSDictionary
                    print(JSON)
                    let ResponseSuccess = JSON.object(forKey: "response")!
                    self.displayAlert(Message: ResponseSuccess as! String, myView:self)


                }


            case .failure( _):

                if let result = response.result.value {
                    stopActivityIndicator()
                    let JSON = result as! NSDictionary
                    print(JSON)
                    let ResponseFail = JSON.object(forKey: "response")!
                    self.displayAlert(Message: ResponseFail as! String, myView:self)
                }
            }


    }

关于ios - 如何使用 swift 4 从数据中使用 Alamofire 多部分发送参数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380508/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4