Send and receive data streams to generate images remotely

Source: Internet
Author: User

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 ");}

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.