Convert an image into a data stream and send it to a remote service. Then, the image is stored in the desired place after receiving the data stream through the server background program.
This method is similar to the image upload function, hoping to provide some garden friends with another way to upload images.
Method for sending data streams
/// <Summary> /// postbinarydata /// </Summary> /// <Param name = "url"> URL to be sent </param> /// <Param name = "bytes"> data stream to be sent </param> // <returns> </returns> Public String postbinarydata (string URL, byte [] bytes ){
// The following is a test example // string url = "http://www.test.com/test.ashx"; // string IMG = httpcontext. current. server. mappath (".. /images/test.jpg "); // byte [] bytes = file. readallbytes (IMG); httpwebrequest wrequest = (httpwebrequest) webrequest. create (URL); wrequest. contenttype = "multipart/form-Data"; wrequest. contentlength = bytes. length; wrequest. method = "Post"; stream = wrequest. getrequeststream (); stream. write (bytes, 0, bytes. length); stream. close (); httpwebresponse wresponse = (httpwebresponse) wrequest. getresponse (); streamreader sreader = new streamreader (wresponse. getresponsestream (), system. text. encoding. utf8); string STR = sreader. readtoend (); sreader. close (); wresponse. close (); Return STR ;}
Method for receiving data streams
Public void getbinarydata () {string imgfile = datetime. now. tostring ("yyyymmddhhmmss") + ". jpg "; string filepath = httpcontext. current. server. mappath (imgfile); // method 1 int lang = httpcontext. current. request. totalbytes; byte [] bytes = httpcontext. current. request. binaryread (Lang); string content = system. text. encoding. utf8.getstring (bytes); filestream fstream = new filestream (filepath, filemode. create, fileaccess. write); binarywriter BW = new binarywriter (fstream); BW. write (bytes); BW. close (); fstream. close (); // method 2 bitmap IMG = new Bitmap (httpcontext. current. request. inputstream); IMG. save (filepath); httpcontext. current. response. write ("OK ");}