This is the code that upload an image but I have to send another POST parameter to know who is the person that upload the image.
var request = NSMutableURLRequest(URL: NSURL(string:"https://www.theweb.com/api/img.php")!)
request.HTTPMethod = "POST"
var boundary = NSString(format: "---------------------------14737809831466499882746641449")
var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
// println("Content Type \(contentType)")
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
request.HTTPBody = dataMeData
var body = NSMutableData.alloc()
// Image
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition: form-data; name=\"userfile\"; filename=\"\(me)\"\\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData)
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)
println("returnString \(returnString!)")
I have tried to add the following:
body.appendData(thePersonWhoUpload) ---> in NSData encoding it in UTF8 but it doesn't work.
So the final question is: how can I send another POST parameter which contains a name?