Swift + ios 9,使用base64图像数据发送数据

时间:2015-10-05 23:09:51

标签: node.js swift post

我已经查看了与在swift上发送NS的帖子请求相关的不同问题。我遇到了以下代码的问题,其中响应服务器甚至没有将其视为发布请求。

我缺少什么:

    var request = NSMutableURLRequest(URL: NSURL(string: url)!)


    request.HTTPMethod = "POST"

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")

    request.addValue("application/json", forHTTPHeaderField: "Accept")



    var imageData = UIImageJPEGRepresentation(img, 0.6)

    var base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) // encode the image



    var err: NSError? = nil

    var params = ["image":[ "content_type": "image/jpeg", "filename":"\(self.riderid).jpg", "file_data": base64String]]

    println("Image params \(params)")

    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions(0), error: &err)!




    var task = session.dataTaskWithRequest(request, completionHandler: { data, response, error -> Void in

        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)

        var err: NSError?

        println("Image Response \(err) \(data)")

        // process the response

        var hoge = JSON(data: data!)

        println("Image Response \(hoge)")



    })

3 个答案:

答案 0 :(得分:0)

任务块后需要task.resume()。

答案 1 :(得分:0)

我知道它已经重播了,但也许它可以帮助某人

var base64String = Detail.image.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    print(base64String)
   var base64String2 = base64String.replacingOccurrences(of: "+", with: "%2B", options: NSString.CompareOptions.literal, range: nil)
    base64String2 = base64String.replacingOccurrences(of: "/", with: "%2F", options: NSString.CompareOptions.literal, range: nil)
    print(base64String2)

做这样的事情

  func insertDoctor_Detailmaster(_ Detail:DoctorDetail, isInsertOrUpdate:String){
        var request:NSMutableURLRequest!
        var postString:String
        var base64String = Detail.image.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
        print(base64String)
       var base64String2 = base64String.replacingOccurrences(of: "+", with: "%2B", options: NSString.CompareOptions.literal, range: nil)
        base64String2 = base64String.replacingOccurrences(of: "/", with: "%2F", options: NSString.CompareOptions.literal, range: nil)
        print(base64String2)

        if(isInsertOrUpdate == "insert"){
            request = NSMutableURLRequest(url: URL(string: link.commonlink + "insert_doctor_master.php")!)
            param = "doctor_id=\(Detail.doctor_id)&first_name=\(Detail.first_name)&last_name=\(Detail.last_name)&mid_name=\(Detail.mid_name)&speciality=\(Detail.speciality)&dob=\(Detail.dob)&gender=\(Detail.gender)&address_id=\(Detail.address_id)&mobile_number=\(Detail.mobile_number)&user_image=\(base64String2)"

            _ = SD.executeChange(sqlStr: "INSERT INTO doctor_master (doctor_id, first_name,last_name, mid_name, dob, gender,doctorImage,speciality, address_id, mobileNumber) VALUES ('\(Detail.doctor_id)','\(Detail.first_name)','\("")','\(Detail.mid_name)','\(Detail.dob)','\(Detail.gender)','\(base64String)','\(Detail.speciality)','\(Detail.mobile_number)','\(Detail.mobile_number)')")

        }else{
            request = NSMutableURLRequest(url: URL(string: link.commonlink + "update_doctor_master.php")!)
            param = "doctor_id=\(Detail.doctor_id)&first_name=\(Detail.first_name)&last_name=\(Detail.last_name)&mid_name=\(Detail.mid_name)&speciality=\(Detail.speciality)&dob=\(Detail.dob)&gender=\(Detail.gender)&address_id=\(Detail.address_id)&mobile_number=\(Detail.mobile_number)&\(base64String2)"

            _ = SD.executeChange(sqlStr: "UPDATE doctor_master SET doctor_id = \(Detail.doctor_id), firstName ='\(Detail.first_name)',last_name = '\("")', mid_name ='\(Detail.mid_name)', dob ='\(Detail.dob)',gender = '\(Detail.gender)',doctorImage='\(base64String)',speciality='\(Detail.speciality)', address_id ='\(Detail.mobile_number)',mobileNumber = '\(Detail.mobile_number)'")


        }
        print(param)

        postString = "q=\(AGS().encrypt(PlainText: param!))"

        request.httpMethod = "POST"

        request.httpBody = postString.data(using: String.Encoding.utf8)
        let session = URLSession.shared
        let task = session.dataTask(with: request as URLRequest) {data,response,error in
            if error != nil {
                print("error=\(error)")
                return
            }
            print("response = \(response)")
            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print(responseString!)
            res.variable.dosage = responseString as! String
        }
        task.resume()
        print(postString)
    }

答案 2 :(得分:0)

确保您的会话对象的配置,并且 JMStudios.jrichardson 说,写 task.resume()< / strong>在任务块之后。