iOS file上传

作者: 码农淏 | 来源:发表于2017-08-03 10:05 被阅读21次

    配置参数

    let data = UIImageJPEGRepresentation(_uploadImage,0.8)!
            let params = ["biaot" : titleTextField.text!, "content" : textView.text, "type" : (adTypeBtn.titleLabel?.text)!] as [String : String]
            
            upload(address: Common.gggl(), data: data, param: params)
    

    上传方法

     func upload(address: String,data: Data, param: [String : String]) {
    
            let model = XUserSaveSuccess.readUserInformation()
            
            let cookie = (model?.session_name)! + "=" + (model?.sessid)!//[NSString stringWithFormat:@"%@=%@", sessionName, sessid];
            let headers = ["Accept" : "application/json", "Content-Type" : "application/json", "Cookie" : cookie, "X-CSRF-Token" : model?.token!]
            printLog(headers)
            
            showHud(in: view, hint: nil, yOffset: 0)
            Alamofire.upload(multipartFormData: { (multipartFormData) in
                
                let imageName = String(describing: NSDate()) + ".png"
                
                //multipartFormData.appendBodyPart(data: ,name: ,fileName: ,mimeType: )这里把图片转为二进制,作为第一个参数
                multipartFormData.append(data, withName: "uploadfile", fileName: imageName, mimeType: "image/jpeg")
    //            multipartFormData.append(URL(string: self.filePath!)!, withName: "image", fileName: "image.png", mimeType: "image/png")
                
                
                //把剩下的两个参数作为字典,利用 multipartFormData.appendBodyPart(data: name: )添加参数,
                //因为这个方法的第一个参数接收的是NSData类型,所以要利用 NSUTF8StringEncoding 把字符串转为NSData
                
                //遍历字典
                for (key, value) in param {
                    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
            }, usingThreshold: 60, to: address, method: HTTPMethod.post, headers: headers as? HTTPHeaders) {[weak self] (encodingResult) in
                
                hideHud()
                
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON(completionHandler: { (response) in
                        if let myJson:[String: String] = response.result.value as? [String : String] {
                            printLog(myJson)
                            printLog(response.result)
                            printLog(response.description)
                            printLog(response.debugDescription)
                            printLog(response.request)
                            printLog(response.response)
                            
                            if myJson["status"]! == "1" {
                                self?.navigationController?.popViewController(animated: true)
                                showHintInKeywindow(hint: "发布成功")
                            } else {
                                showHintInKeywindow(hint: "发布失败")
                            }
                        }
                    })
                case .failure(let error):
                    printLog(error)
                    showHintInKeywindow(hint: "请检查您的网络")
                }
            }
            
        }
    

    相关文章

      网友评论

        本文标题:iOS file上传

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