Loading Batch Image Using Request Body
I was trying to upload images from Swift app to PHP based server. I have put together some code and will try to use it, but the download was not successful. Listed below are the functions to load images with an additional request body that I created along with the response I received.
Note . The server is running and I have tested the RestClient application for testing. It takes in multipart/form-data
and accepts all requests in the body.
Boot function
func imageUploadRequest(){
let parameters = [
"activity1": selectedActivity+1,
"activity2": selectedActivity2+1,
"activity3": selectedActivity3+1,
"activity_id": selectedActID
]
let boundary = generateBoundaryString()
let url = NSURL(string: "http://myServerURL/uploadImage")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let newImageData = UIImageJPEGRepresentation(self.imageView.image, 1)
let newImage2Data = UIImageJPEGRepresentation(self.imageView2.image, 1)
let newImage3Data = UIImageJPEGRepresentation(self.imageView3.image, 1)
request.HTTPBody = createBodyWithParameters(parameters, imageDataKey: ["image1": newImageData, "image2": newImage2Data, "image3": newImage3Data], boundary: boundary)
.
.
.
}
CreateBodyWithParameters function
func createBodyWithParameters(parameters: [String: Int]?, imageDataKey: [String: NSData]?, boundary: String) -> NSData {
var body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("โ\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
if imageDataKey != nil {
for (key, value) in imageDataKey! {
let filename = "user-profile.jpg"
let mimetype = "image/jpg"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendData(value)
body.appendString("\r\n")
}
}
body.appendString("\r\n-โ\(boundary)-โ\r\n")
return body
}
The answer I got was:
<NSHTTPURLResponse: 0x170439cc0> { URL: http://myServerURL/uploadImage } { status code: 500, headers {
"Cache-Control" = "no-cache";
Connection = close;
"Content-Type" = "text/html";
Date = "Thu, 04 Jun 2015 01:52:46 GMT";
Server = Apache;
"Set-Cookie" = "laravel_session=eyJpdiI6IjlyaWRKMjdBbTZiWWluSGk3QU9lcXc9PSIsInZhbHVlIjoiWlwvOHdtcURjOHlSTWR3MmtYNm0rbHlEaWJKSTMrZk1hWkVvSHpzTVdrcE5SQTl2bFkxemlJYUNLYkZMZjJxdEFCMFwvWENlMzFReW9GU3Jkam5XSmxvZz09IiwibWFjIjoiYWJmYWU2MDU2MGRkNzM1MjcyNWM1YjFjMDJjZjA5MmNjNjA4MzA3ZGE5MWMzNmJiZmE3ZTg3Y2I2OWI4N2E4NiJ9; expires=Thu, 04-Jun-2015 03:52:48 GMT; Max-Age=7200; path=/; httponly";
} }
source to share
Ultimately, you will need to check your server for the 500 errors you receive. If you suspect that this is a multiplier, I notice that your first border has one dash, there should be two:
body.appendString("-โ\(boundary)\r\n")
Also, not sure if this is the problem, but try using a different cutoff value if you have multiple images. See the latest example here .
The creation of multipart forms is a pain in my opinion. If I were you, I would use a connection library like Alamofire or AFNetworking.
source to share