0

I am unable to upload my image on server. I am using multpart form data.I am selecting Image from ImagePicker controller.Everything is set Imagerpicker delegate each and everything .after uploading image it is not shoeing any error but image is not uploading on server.and also when I select image using imagepicker it shows a error like Creating an image format with an unknown type is an error but my app does not crashes

this is the code that I am using in swift3 for sending image. Here is my Code that I am Using.

viewcontroller.swift //I am not writnig full code but from the following action of button i am calling that function

    button Action{
    // my image
    let image = userImage.image
    let unwrappedImage = image!
    let data = UIImageJPEGRepresentation(unwrappedImage, 1.0)
    let params = ["user_dob":dob, "user_gender":"1", "user_name":   username, "user_status":userstatus] as NSDictionary
    self.UploadRequest(image:data,parameters:params)

    }

my function for uploading image and data

    func UploadRequest(image:NSData,parameters:NSDictionary){
    let url = NSURL(string: "myURL")
    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "POST"
    let boundary = generateBoundaryString()
    let secretKey = defaults.string(forKey: "secretKey")

    request.setValue(secretKey, forHTTPHeaderField: "auth_key")

request.setValue("multipart/form-data; boundary=\(boundary)",forHTTPHeaderField: "Content-Type")

    let body = NSMutableData()

    body.append("--\(boundary)\r\n".data(using: .utf8)!)

    body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)

    body.append("hi\r\n".data(using: .utf8)!)

    body.append("--\(boundary)\r\n".data(using: .utf8)!)

    //body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: .utf8)!)

    //body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: .utf8)!)

    body.append(image as Data)

    body.append("\r\n".data(using: String.Encoding.utf8)!)

    for(key,value) in parameters{
    body.append("--\(boundary)\r\n".data(using: .utf8)!)
    body.append("Content-Disposition:form-data;      name=\"file\";filename=\"\(key)\"\r\n\r\n\(value)".data(using: .utf8)!)   
   }
    body.append("--\(boundary)--\r\n".data(using: .utf8)!)



    let session = URLSession.shared

    let task = session.dataTask(with: request as URLRequest) {

        (
        data, response, error) in

        guard let _:NSData = data as NSData?, let _:URLResponse = response  , error == nil else
        {
            print("error")

            return
    }let dataString =NSString(data:data!,encoding:String.Encoding.utf8.rawValue)

        print(dataString)

    }

    task.resume()


}
  • You're commented out the `Content-Disposition` before writing the `image data, but that's needed. – Rob Nov 13 '16 at 08:58
  • Unrelated, rather than casting `NSData` and `NSMutableURLRequest` to `Data` and `URLRequest`, just use those types. The intent of the the new types is to retire these `NS` types. – Rob Nov 13 '16 at 08:59
  • Can you help me out for this Rob? – Akash vishwakarma Nov 13 '16 at 09:01
  • See http://stackoverflow.com/a/26163136/1271826 – Rob Nov 13 '16 at 09:02
  • the same problem is also occuring befre adding comment – Akash vishwakarma Nov 13 '16 at 09:03
  • What was the mimetype value that you sent? Are you sure you got all of the field names right? Also, you're sending the parameters as filenames, but you should include them like your `test` field, without filename... – Rob Nov 13 '16 at 09:09
  • Preparing multipart requests isn't hard, but it's awfully finicky. If you don't use the code from that [other answer](http://stackoverflow.com/a/26163136/1271826), you might just consider using [Alamofire](https://github.com/Alamofire/Alamofire) which abstracts you away from these gory details. – Rob Nov 13 '16 at 09:11

0 Answers0