WebClient implement multi-file/text upload simultaneously

Source: Internet
Author: User
Tags httpcontext

 Public classcreatebytes {Encoding Encoding=Encoding.UTF8; /**/        /// <summary>        ///stitching all binary arrays into an array/// </summary>        /// <param name= "Bytearrays" >Array</param>        /// <returns></returns>        /// <remarks>Plus end Border .</remarks>         Public byte[] joinbytes (ArrayList bytearrays) {intLength =0; intReadlength =0; //Plus end Border .            stringEndboundary = boundary +"--\r\n";//End Border            byte[] Endboundarybytes =encoding.            GetBytes (endboundary);            Bytearrays.add (endboundarybytes); foreach(byte[] Binchbytearrays) {Length+=b.length; }            byte[] bytes =New byte[length]; //Traversal Replication//            foreach(byte[] Binchbytearrays)                {b.copyto (bytes, readlength); Readlength+=b.length; }            returnbytes; }         Public BOOLUploaddata (stringUploadurl,byte[] Bytes, out byte[] responsebytes) {WebClient WebClient=NewWebClient (); WEBCLIENT.HEADERS.ADD ("Content-type", ContentType); Try{responsebytes=webclient.uploaddata (uploadurl, bytes); return true; }            Catch(WebException ex) {Stream resp=Ex.                Response.getresponsestream (); ResponseBytes=New byte[Ex.                Response.contentlength]; Resp. Read (ResponseBytes,0, responsebytes.length); }            return false; }        /**/        /// <summary>        ///get a normal form area binary array/// </summary>        /// <param name= "FieldName" >Table sole name</param>        /// <param name= "Fieldvalue" >form Values</param>        /// <returns></returns>        /// <remarks>        ///-----------------------------7d52ee27210a3c\r\ncontent-disposition:form-data; name=\ "table sole name \" \r\n\r\n Form value \ r \ n /c13>/// </remarks>         Public byte[] Createfielddata (stringFieldName,stringfieldvalue) {            stringTexttemplate = boundary +"\r\ncontent-disposition:form-data; name=\ "{0}\" \r\n\r\n{1}\r\n"; stringText =String.Format (Texttemplate, FieldName, Fieldvalue); byte[] bytes =encoding.            GetBytes (text); returnbytes; }        /**/        /// <summary>        ///get File Upload form area binary array/// </summary>        /// <param name= "FieldName" >Table sole name</param>        /// <param name= "filename" >file name</param>        /// <param name= "ContentType" >File Type</param>        /// <param name= "ContentLength" >file Length</param>        /// <param name= "Stream" >file Stream</param>        /// <returns>Binary Arrays</returns>         Public byte[] Createfielddata (stringFieldName,stringFileNamestringContentType,byte[] filebytes) {            stringEnd ="\ r \ n"; stringTexttemplate = boundary +"\r\ncontent-disposition:form-data; name=\ "{0}\"; filename=\ "{1}\" \r\ncontent-type: {2}\r\n\r\n"; //Header Data            stringdata =String.Format (texttemplate, fieldName, filename, contentType); byte[] bytes =encoding.            GetBytes (data); //Tail Data            byte[] Endbytes =encoding.            GetBytes (end); //the synthesized array            byte[] FieldData =New byte[Bytes. Length + filebytes.length +Endbytes.length]; bytes. CopyTo (FieldData,0);//Header DataFilebytes.copyto (FieldData, bytes. Length);//Binary data for filesEndbytes.copyto (FieldData, bytes. Length + filebytes.length);//\ r \ n            returnFieldData; }        #regionProperty Public stringBoundary {Get            {                string[] Barray, Ctarray; stringContentType =ContentType; Ctarray= Contenttype.split (';'); if(ctarray[0]. Trim (). ToLower () = ="Multipart/form-data") {Barray= ctarray[1]. Split ('='); return "--"+ barray[1]; }                return NULL; }        }         Public stringContentType {Get            {                if(HttpContext.Current = =NULL)                {                    return "Multipart/form-data, boundary=---------------------------7d5b915500cee"; }                returnHttpContext.Current.Request.ContentType; }        }        #endregion    }

The method is called as follows:

Createbytes cb =Newcreatebytes (); //all form dataArrayList Bytesarray =NewArrayList (); //Normal FormsBytesarray.add (CB. Createfielddata ("Jsondatastr","")); //Read file streamFileStream fs =NewFileStream ("Wolfy. JPG", FileMode.Open, FileAccess.Read, FileShare.Read); stringContentType ="Application/octet-stream"; byte[] Filebytes =New byte[FS.            Length]; Fs. Read (Filebytes,0, Convert.ToInt32 (fs.            Length)); //file FormBytesarray.add (CB. Createfielddata ("file1","Wolfy. JPG", ContentType, filebytes)); Bytesarray.add (CB. Createfielddata ("file2","wolfy1. JPG", ContentType, filebytes)); //synthesize all forms and generate binary arrays            byte[] bytes =CB.            Joinbytes (Bytesarray); //What's returned            byte[] responsebytes; BOOLUploaded = cb. Uploaddata ("Http://localhost:8700/api/System/ImgUpload", Bytes, outResponseBytes);

WebClient implement multi-file/text upload simultaneously

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.