在swift 4.1中将图像上传到服务器端。图片类型出现错误:

时间:2018-08-08 06:01:58

标签: swift

我要在服务器上上传图像。我的以下代码在图像类型错误:传递参数。我使用了NSUrlSession并上传了图片。以下代码使用转换器从Objective-C转换为Swift。我不知道为什么会收到这个错误。我是新手。我是Objective-C开发人员。现在,我正在学习迅速,如果有任何错误,也请给我建议。预先感谢。

 func uploadImage1() {
    diagnosis = ""

    let dict = UserDefaults.standard.dictionary(forKey: "login")! as NSDictionary
    let token = dict.object(forKey: "Token")
    let Doctor_Id = dict.object(forKey: "UserId")
    let patientIdNum = indfoDict.object(forKey: "Patient_Id") as! NSNumber
    let patientId = patientIdNum.stringValue

    doctorID = Doctor_Id as! String

    let BoundaryConstant = "----------V2ymHFg03ehbqgZCaKO6jy"
    // var FileParamConstant = "filedata"
    let requestURL = URL(string: "https://www.stopbadcheck.com/IntegrationService.svc/UploadImage")
    let request = NSMutableURLRequest()
    request.cachePolicy = .reloadIgnoringLocalCacheData
    request.httpShouldHandleCookies = false
    request.timeoutInterval = 30
    request.httpMethod = "POST"
    if let aKey = token {
        request.addValue("\(aKey)", forHTTPHeaderField: "Token")
    }
    let contentType = "multipart/form-data; boundary=\(BoundaryConstant)"
    request.setValue(contentType, forHTTPHeaderField: "Content-Type")
    var body = Data()
    if (image != nil) {
        imageData = UIImageJPEGRepresentation(image!, 1)
    }
    if (imageData != nil) {
        if let anEncoding = "--\(BoundaryConstant)\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if (image != nil) {
            if let anEncoding = "Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
                body.append(anEncoding)
            }
            print("body image input ---> \(body)")
        }
        if let anEncoding = "Content-Type: image/jpeg\r\n\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        body.append(imageData!)
        if let anEncoding = "\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        // param1
        if let anEncoding = "--\(BoundaryConstant)\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "Content-Disposition: form-data; name=\"Patient_Id\"\r\n\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = patientId.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }

        // param2
        if let anEncoding = "--\(BoundaryConstant)\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "Content-Disposition: form-data; name=\"Doctor_Id\"\r\n\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = doctorID.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }

        // param3
        if let anEncoding = "--\(BoundaryConstant)\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "Content-Disposition: form-data; name=\"Diagnosis_Desciption\"\r\n\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = diagnosis.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        if let anEncoding = "--\(BoundaryConstant)--\r\n".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
            body.append(anEncoding)
        }
        request.httpBody = body
        let postLength = "\(UInt(body.count))"
        request.setValue(postLength, forHTTPHeaderField: "Content-Length")
        request.url = requestURL
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in

            if let data = data {
                // do what you want in success case

                print(data)
                do {
                    let json = try JSONSerialization.jsonObject(with: data) as! Dictionary<String, AnyObject>
                    print("Json--->", json)

                    DispatchQueue.main.async {

                        var dict = NSDictionary()
                        dict = json as Dictionary as NSDictionary
                        print("Dict--->",dict)

                    }
                } catch {
                    print("error")
                }
            }
        })
        dataTask.resume()
    }
}

0 个答案:

没有答案