ASP. NET upload file background sent to other URLs via binary stream save

Source: Internet
Author: User
Tags md5 digest md5 encryption

The actual situation usually has a separate site for static files, tablets, Office documents, and so on. The operation of a site requires uploading files to site B,

Here's a way to pass the Uploaddata method of the System.Net.WebClient class.

Uploadfile.aspx HTML:

<formID= "Form1"runat= "Server">    <Div>        <Asp:fileuploadrunat= "Server"ID= "Fileup"AllowMultiple= "true" />         <Asp:buttonrunat= "Server"ID= "BtnLoad"Text= "Upload"OnClick= "Btnload_click" />    </Div>    </form>

UploadFile.cs Background Code

 protected voidBtnload_click (Objectsender, EventArgs e) {            if(fileup.hasfile) {varList =Fileup.postedfiles; foreach(varIteminchlist) {                    stringFiledoc = System.IO.Path.GetExtension (item. FileName). ToLower ();//suffix name                    intFilelength = (int) Item. Inputstream.length;//inputstream.length;                    byte[] Bytefile =New byte[Filelength]; Item. Inputstream.read (Bytefile,0, filelength); Item. Inputstream.seek (0, System.IO.SeekOrigin.Begin); After reading the stream, set the location of the current stream, because MD5 encryption also needs to use the streamstringMd5value =Com.MD5.MyMD5.getMd5Hash (item. InputStream); Get the upload file stream MD5stringURL =string. Format ("http://localhost:4884/SaveFile.ashx?name="+ Md5value +"&ext="+Filedoc); Save the path to the processing file for the stream System.Net.WebClient WC=NewSystem.Net.WebClient (); byte[] BTS = WC. Uploaddata (URL,"POST", Bytefile); Wc.                    Dispose (); stringFilePath =System.Text.Encoding.UTF8.GetString (BTS);//Get savefile.ashx return}} }
Com.MD5.MyMD5.getMd5Hash method See: My C # MD5 digest algorithm, hash algorithm

SAVEFILE.ASHX processing Program

 /// <summary>    ///Summary description of SaveFile/// </summary>     Public classSavefile:ihttphandler { Public voidProcessRequest (HttpContext context) {Lock(context. Request.inputstream) {HttpRequest Request=context.                Request; stringPath ="";stringFilenname =""; stringRe =""; if(Request.HttpMethod.ToLower () = ="Post")                {                    Try                    {                        using(request.inputstream) {stringName = request["name"]; strings = request["ext"]; byte[] bytes =New byte[Request.InputStream.Length]; Request.InputStream.Read (Bytes,0, bytes.                             Length); Request.InputStream.Seek (0, Seekorigin.begin); // Set the position of the current stream                              //Request.InputStream.Position = 0;Request.InputStream.Flush ();                            Request.InputStream.Close ();                            Request.InputStream.Dispose (); Random Rnd=NewRandom (); intn = rnd. Next ( +,9999); //filenname = "/file/" + n.tostring () + "-" + DateTime.Now.ToString ("yyyymmddhhmmssss") + S;Filenname ="/file/"+name +s; Path=context.                            Server.MapPath (Filenname); FileStream FS=NewFileStream (path, filemode.create); BinaryWriter BW=NewBinaryWriter (FS); Bw.                            Write (bytes); Bw.                            Close (); Fs.                            Close (); Re=Filenname;//system.threading.thread.sleep ( -); }                    }                    Catch(Exception ex) {}} context. Response.ContentType="Text/plain"; Context.                Response.Write (re); Context.            Response.End (); }        }         Public BOOLisreusable {Get            {                return false; }        }    }



ASP. NET upload file background sent to other URLs via binary stream save

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.