美文网首页
swift moya MultipartFormData方式传递

swift moya MultipartFormData方式传递

作者: 稀客花千树 | 来源:发表于2019-06-12 09:46 被阅读0次
                let formData1 = MultipartFormData(provider: .data((parameters["name"] as! String).data(using: .utf8)!), name: "name")
                let formData2 = MultipartFormData(provider: .data((parameters["place"] as! String).data(using: .utf8)!), name: "place")
                let formData3 = MultipartFormData(provider: .data((parameters["desc"] as! String).data(using: .utf8)!), name: "desc")
                let formData4 = MultipartFormData(provider: .data((String(parameters["holdNums"] as! Int)).data(using: .utf8)!), name: "holdNums")
                let formData5 = MultipartFormData(provider: .data((parameters["type"] as! String).data(using: .utf8)!), name: "type")
                let formData6 = MultipartFormData(provider: .data(parameters["image"] as! Data), name: "file", fileName: "files.png", mimeType: "image/png")
                var multipartData = [formData1,formData2,formData3,formData4,formData5,formData6]
                if (parameters["approve"] as! Bool) == true {
                    multipartData.append(MultipartFormData(provider: .data(String((parameters["approve"] as! Bool)).data(using: .utf8)!), name: "approve"))
                }
                if (parameters["devices"] as! NSArray).count != 0 {
                    multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["devices"] as! [String] as NSArray)).data(using: .utf8)!), name: "devices"))
                }
                if (parameters["openUsers"] as! NSArray).count != 0 {
                    multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["openUsers"] as! [String] as NSArray)).data(using: .utf8)!), name: "openUsers"))
                }
                if (parameters["openDep"] as! NSArray).count != 0 {
                    multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["openDep"] as! [String] as NSArray)).data(using: .utf8)!), name: "openDep"))
                }
                if (parameters["firstVerifier"] as! String) != "" {
                    multipartData.append(MultipartFormData(provider: .data((parameters["firstVerifier"] as! String).data(using: .utf8)!), name: "firstVerifier"))
                }
                if (parameters["secondVerifier"] as! String) != "" {
                    multipartData.append(MultipartFormData(provider: .data((parameters["secondVerifier"] as! String).data(using: .utf8)!), name: "secondVerifier"))
                }
                return .uploadMultipart(multipartData)
    
    func getJSONStringFromArray(array: NSArray) -> String {
        guard JSONSerialization.isValidJSONObject(array) else {
            return ""
        }
        let data: NSData! = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData
        let JSONString = NSString(data: data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
    

    相关文章

      网友评论

          本文标题:swift moya MultipartFormData方式传递

          本文链接:https://www.haomeiwen.com/subject/fcckfctx.html