Asp.net post any data type to the website-Dangdang API uploads XML files

Source: Internet
Author: User
The following Code You can post any type of file, mainly to convert the data stream to base64 format. // ------------------------------ send part of the response string url = "http: // localhost/IM/upfile? AA = 5 "; // the address of the page sent to httpwebrequest Req = (httpwebrequest) webrequest. create (URL); // read an image filestream FS = new filestream (server. mappath ("~ /Netbasics .doc "), system. io. filemode. open, system. io. fileaccess. read); byte [] filecontent = new byte [FS. length]; FS. read (filecontent, 0, filecontent. length); FS. close (); FS. dispose (); // convert the image to a base64 encoded stream string a = convert. tobase64string (filecontent); // read the base64 encoded stream and send byte [] requestbytes = system. text. encoding. default. getbytes (a); req. method = "Post"; req. contenttype = "application/X-WWW-form-urlencod Ed "; req. contentlength = requestbytes. length; stream requeststream = req. getrequeststream (); requeststream. write (requestbytes, 0, requestbytes. length); requeststream. close (); // receives the response parameters to string backstr httpwebresponse res = (httpwebresponse) req. getresponse (); streamreader sr = new streamreader (res. getresponsestream (), system. text. encoding. default); string backstr = sr. readtoend (); Sr. close (); Res. cl Ose (); // output parameter response. write (backstr); // --------------------------------------------------- receiving part --------------------------------- // The received parameter string BB = request. querystring ["AA"]; encoding myencoding = encoding. getencoding ("UTF-8"); // receives the transmitted data stream resstream = request. inputstream; byte [] filecontent = new byte [resstream. length]; // read the data stream into the byte array resstream. read (filecontent, 0, filecontent. length); // Array Convert to string to convert base64 using string a = myencoding. getstring (filecontent); // decrypt the string to the byte array byte [] filecontent2 = convert. frombase64string (a); // write to the directory file. writeallbytes (server. mappath ("~ /"+ BB +". Doc "), filecontent2); // return value response. Write (" OK "); response. End ();

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.