Monday, December 3, 2018

Upload image with multipart form-data iOS in Swift

i am having a problem with uploading image with multipart-form



here is my code i used from this answer



    var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"


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

var body = NSMutableData()

if self.img.image != nil {
var imageData = UIImagePNGRepresentation(self.img.image)

if imageData != nil {
body.appendString("--\(boundary)\r\n")

body.appendString("Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n")
body.appendString("Content-Type: image/png\r\n\r\n")
body.appendData(imageData!)
body.appendString("\r\n")
}

}

body.appendString("--\(boundary)--\r\n")
request.setValue("\(body.length)", forHTTPHeaderField:"Content-Length")

request.HTTPBody = body


then i use NSURLSession to apply the request



the server says that i didn't choose image to upload i only want to upload the image for now



do i have to use paths of images to upload any image or it's data is enough?



do i miss any thing , any help to understand this ?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...