Upload images in POST mode and images in post Mode

Source: Internet
Author: User

Upload images in POST mode and images in post Mode

Set enctype = multipart/form-data

<form  enctype="multipart/form-data"></form>

 

Custom MultipartFormData class (for Versions later than 4.5, it seems that this class name is missing)

/// <Summary> /// multipart/form-data Type // </summary> public class MultipartFormData {public static string ContentType = "multipart/form-data; boundary = A300x "; private string headerStream =" -- A300x \ r \ nContent-Disposition: form-data; name = \ "{0 }\"; filename = \ "{1} \" \ r \ nContent-Type: application/octet-stream \ r \ n "; private string footerString = "-- A300x -- \ r \ n"; private string compart = "\ r \ n "; Private string headerString = "-- A300x \ r \ nContent-Disposition: form-data; name = \" {0} \ "\ r \ n "; private List <byte> lists = new List <byte> (); private byte [] footerBytes; private byte [] comparts; public MultipartFormData () {footerBytes = Encoding. UTF8.GetBytes (footerString); comparts = Encoding. UTF8.GetBytes (compart);} public byte [] Buffer {get {return lists. toArray () ;}} private bool IsEnd = False; /// <summary> ///// </summary> public void End () {if (! IsEnd) {IsEnd = true; // lists. addRange (footerBytes. toList (); this. addRanges (footerBytes);} public void AddRanges (byte [] array) {foreach (byte B in array) lists. add (B );} /// <summary> /// add HTTP content to the object set serialized to multiple/form data MIME types /// </summary> /< param name =" content "> HTTP content to be added to the set </param> // <param name =" name "> name of the HTTP content to be added </param> public void Add (string content, string name) {byte [] headerBytes = Encoding. UTF8.GetBytes (String. format (headerString, name); this. addRanges (headerBytes); byte [] buffer = Encoding. UTF8.GetBytes (content); this. addRanges (buffer); this. addRanges (comparts );} /// <summary> /// add HTTP content to the object set serialized to multiple/form data MIME types /// </summary> /< param name =" content "> HTTP content to be added to the collection </param> /// <param name =" name "> name of the HTTP content to be added </param> /// <param name = "fileName"> File name of the HTTP content to be added to the set </param> public void Add (Stream content, string name, string fileName) {byte [] headerBytes = Encoding. UTF8.GetBytes (String. format (headerStream, name, fileName); this. addRanges (headerBytes); byte [] buffer = new byte [content. length]; content. read (buffer, 0, buffer. length); content. seek (0, SeekOrigin. begin); this. addRanges (buffer); this. addRanges (comparts );} /// <summary> /// add HTTP content to the object set serialized to multiple/form data MIME types /// </summary> /< param name =" content "> HTTP content to be added to the collection </param> /// <param name =" name "> name of the HTTP content to be added </param> /// <param name = "fileName"> File name of the HTTP content to be added to the collection </param> public void Add (byte [] content, string name, string fileName) {byte [] headerBytes = Encoding. UTF8.GetBytes (String. format (headerStream, name, fileName); this. addRanges (headerBytes); this. addRanges (content); this. addRanges (comparts );}}
 
 
Upload Image Code
/// <Summary> // form-data type transfer data // </summary> private MultipartFormData multiparFormData = new MultipartFormData (); /// <summary> /// corresponding to the key value /// </summary> /// <param name = "key"> </param> /// <param name = "value"> </param> public void AddFormData (string key, string value) {this. multiparFormData. add (value, key );} /// <summary> /// pass a strem type /// </summary> /// <param name = "key"> </param> /// <param name = "value"> </param> // <param name = "fileName"> </param> public void AddFormData (string key, stream value, string fileName) {this. multiparFormData. add (value, key, fileName );} /// <summary> /// pass a byte [] type // </summary> /// <param name = "key"> </param> /// <param name = "value"> </param> // <param name = "fileName"> </param> public void AddFormData (string key, byte [] value, string fileName) {this. multiparFormData. add (value, key, fileName);} private static CookieContainer m_Cookie = new CookieContainer (); /// <summary> /// POST form-data Request Message // </summary> /// <param name = "url"> </param> // /<param name = "isHttps"> </param> // <returns> </returns> public string PostFromData (string url, bool isHttps) {try {multiparFormData. end (); HttpWebRequest request = (HttpWebRequest) HttpWebRequest. create (url); request. method = "POST"; request. contentType = MultipartFormData. contentType; request. cookieContainer = m_Cookie; // set the cookie of the previous access page to keep the session request. contentLength = multiparFormData. buffer. length; Stream stream = request. getRequestStream (); stream. write (multiparFormData. buffer, 0, multiparFormData. buffer. length); stream. close (); WebResponse response = request. getResponse (); Stream streamResponse = response. getResponseStream (); StreamReader sr = new StreamReader (streamResponse); return sr. readToEnd ();} catch (Exception ex) {Log. writeLog ("POST form-data Request message:" + ex. message); return "error ";}}

Related Article

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.