I was trying to upload images from Swift app to a PHP based server. I have gathered some code related and try to use it but the upload was unsuccessful. The following are the functions for uploading images with additional request body that I have made, along with the response I got.
Note : The server is working, and I have tried with RestClient app for testing. It does accept in multipart/form-data
and accept all the requests in body.
Upload 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
}
Response I received:
<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";
} }
Ultimately you'll have to check your server for the 500 error you're getting. If you suspect its the multipart, I notice that youre first boundary has one dash, there should be two:
body.appendString("-–\(boundary)\r\n")
Also, not sure if this is an issue, but try using another boundary value if you have multiple images. See the last example here .
building multipart forms is a pain in my opinion. If I were you I would use a connection library like Alamofire or AFNetworking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.