windows phone 7 - WP7 - POST form with an image -
i need send image windows phone 7 e-mail addresses. use class submit text values php script, wich parses data , sends formatted e-mail addresses. problem can't figure out how send image script, attach image e-mail. php script can changed in way. if have image object, how can change class allow sending images?
public class postsubmitter { public string url { get; set; } public dictionary<string, string> parameters { get; set; } public postsubmitter() { } public void submit() { // prepare web request... httpwebrequest myrequest = (httpwebrequest)webrequest.create(url); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.begingetrequeststream(new asynccallback(getrequeststreamcallback), myrequest); } private void getrequeststreamcallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; system.io.stream poststream = request.endgetrequeststream(asynchronousresult); // prepare parameters string string parametersstring = ""; foreach (keyvaluepair<string, string> parameter in parameters) { parametersstring = parametersstring + (parametersstring != "" ? "&" : "") + string.format("{0}={1}", parameter.key, parameter.value); } byte[] bytearray = system.text.encoding.utf8.getbytes(parametersstring); // write request stream. poststream.write(bytearray, 0, parametersstring.length); poststream.close(); // start asynchronous operation response request.begingetresponse(new asynccallback(getresponsecallback), request); } private void getresponsecallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(asynchronousresult); stream streamresponse = response.getresponsestream(); streamreader streamread = new streamreader(streamresponse); string responsestring = streamread.readtoend(); // close stream object streamresponse.close(); streamread.close(); // release httpwebresponse response.close(); //action<string> act = new action<string>(displayresponse); //this.dispatcher.begininvoke(act, responsestring); } i use class in way:
dictionary<string, string> data = new dictionary<string, string>() { {"nom", nom.text}, {"cognoms", cognoms.text}, {"email", email.text}, {"telefon", telefon.text} }; postsubmitter post = new postsubmitter() { url = "http://example.com/parserscript.php", parameters = data }; post.submit(); thank much!
i've converted above code following, i'm sure help:
public class postsubmitter { public string url { get; set; } public dictionary<string, object> parameters { get; set; } string boundary = "----------" + datetime.now.ticks.tostring(); public postsubmitter() { } public void submit() { // prepare web request... httpwebrequest myrequest = (httpwebrequest)webrequest.create(new uri(url)); myrequest.method = "post"; myrequest.contenttype = string.format("multipart/form-data; boundary={0}", boundary); myrequest.begingetrequeststream(new asynccallback(getrequeststreamcallback), myrequest); } private void getrequeststreamcallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; stream poststream = request.endgetrequeststream(asynchronousresult); writemultipartobject(poststream, parameters); poststream.close(); request.begingetresponse(new asynccallback(getresponsecallback), request); } private void getresponsecallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(asynchronousresult); stream streamresponse = response.getresponsestream(); streamreader streamread = new streamreader(streamresponse); streamresponse.close(); streamread.close(); // release httpwebresponse response.close(); } public void writemultipartobject(stream stream, object data) { streamwriter writer = new streamwriter(stream); if (data != null) { foreach (var entry in data dictionary<string, object>) { writeentry(writer, entry.key, entry.value); } } writer.write("--"); writer.write(boundary); writer.writeline("--"); writer.flush(); } private void writeentry(streamwriter writer, string key, object value) { if (value != null) { writer.write("--"); writer.writeline(boundary); if (value byte[]) { byte[] ba = value byte[]; writer.writeline(@"content-disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentphoto.jpg"); writer.writeline(@"content-type: application/octet-stream"); //writer.writeline(@"content-type: image / jpeg"); writer.writeline(@"content-length: " + ba.length); writer.writeline(); writer.flush(); stream output = writer.basestream; output.write(ba, 0, ba.length); output.flush(); writer.writeline(); } else { writer.writeline(@"content-disposition: form-data; name=""{0}""", key); writer.writeline(); writer.writeline(value.tostring()); } } } } to convert image camera byte array i've used follwing:
private void photochoosertask_completed(object sender, photoresult e) { try { bitmapimage image = new bitmapimage(); image.setsource(e.chosenphoto); foto.source = image; using (memorystream ms = new memorystream()) { writeablebitmap btmmap = new writeablebitmap(image); // write image stream extensions.savejpeg(btmmap, ms, image.pixelwidth, image.pixelheight, 0, 100); bytearray = ms.toarray(); } } catch (argumentnullexception) { /* nothing */ } } and use class way:
dictionary<string, object> data = new dictionary<string, object>() { {"nom", nom.text}, {"cognoms", cognoms.text}, {"email", email.text}, {"telefon", telefon.text}, {"comentari", comentari.text}, {"foto", bytearray}, }; postsubmitter post = new postsubmitter() { url = "http://example.com/parserscript.php", parameters = data}; post.submit(); i don't know if it's best way send image phone server, couldn't find anything, made own class reading , that, , has taken me several days. if wants improve code or write comment welcomed.
Comments
Post a Comment