I am trying to upload an image to the backend client using swift. The problem is that I cannot get the correct formatting for httpbody. I do not want to use a multi-page form for uploading, because I do not know how to handle it on the server.
Here is the code that I have .. it doesn’t work when I view the image online, which it doesn’t display, and it’s only like 70 kb, which I know is definitely not like a big image.
var bodyString: String = "session_id=\(session_id)&location_id=\(location_id)" bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! var body = NSMutableData.alloc() body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) if image != nil{ var imageData = UIImageJPEGRepresentation(image,0.5) body = NSMutableData.alloc()
UPDATE: so I decided to go for the base64 route, but it still doesn't work, I think because I encode it as ntf8string, is this the right way to do this?
var imageData = UIImageJPEGRepresentation(image,0.5) var imageDataString = imageData.base64EncodedStringWithOptions(.allZeros) body = NSMutableData.alloc() bodyString = "session_id=\(session_id)&location_id=\(location_id)&tag_type=\(tag_type)&image_data=\(imageDataString)" bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
and on the backend I decode it as:
image_data_decoded = base64.b64decode(image_data)
ios swift network-programming
Tyler May 2, '15 at 19:01 2015-05-02 19:01
source share