mardi 14 avril 2015

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: application/octet-stream\r\n\r\n")
body.appendData(imageData!)
body.appendString("\r\n")
}

}

body.appendString("--\(boundary)--\r\n")

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 ?


Aucun commentaire:

Enregistrer un commentaire