REST Windows Phone Photo upload -
i'm trying upload photo rest api in windows phone 7 application using restsharp gets/posts.
post parameters follows:
photo: photo, encoded multipart/form-data
photo_album_id: identifier of existing photo album, may event or group album
i've created request, every time "{\"details\":\"missing photo parameter\",\"problem\":\"the api request malformed\"}\n
my photo parameter looks this:
"---------------------------8cd9bfbafb3ca00\r\ncontent-disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\"\r\ncontent-type: image/jpg\r\n\r\n(some binary junk listed here)\r\n-----------------------------8cd9bfbafb3ca00--"
i'm not quite sure if it's problem how i'm presenting binary data image (currently in phototaskcompleted event, read contents of e.chosenphoto byte[] , pass helper method create form data) or if don't create form correctly.
i'm trying simple possible, can refactor once know how works.
void imageobtained(object sender, photoresult e) { var photo = readtoend(e.chosenphoto); var form = postform(photo); var request = new requestwrapper("photo", method.post); request.addparameter("photo_album_id", _album.album_id); request.addparameter("photo", form); request.client.executeasync<object>(request, (response) => { var s = response.data; }); } private string createboundary() { return "---------------------------" + datetime.now.ticks.tostring("x"); } private string postform(byte[] data) { string boundary = createboundary(); stringbuilder post = new stringbuilder(); post.append(boundary); post.append("\r\n"); post.append("content-disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\""); post.append("\r\n"); post.append("content-type: image/jpg"); post.append("\r\n\r\n"); post.append(convertbytestostring(data)); post.append("\r\n"); post.append("--"); post.append(boundary); post.append("--"); return post.tostring(); } public static string convertbytestostring(byte[] bytes) { string output = string.empty; memorystream stream = new memorystream(bytes); stream.position = 0; using (streamreader reader = new streamreader(stream)) { output = reader.readtoend(); } return output; }
hammock windows phone makes real simple. add file request using addfile
method , pass photo stream.
var request = new restrequest("photo", webmethod.post); request.addparameter("photo_album_id", _album.album_id); request.addfile("photo", filename, e.chosenphoto);
Comments
Post a Comment