C # upload file (ii) Saving a file using a binary stream

Source: Internet
Author: User

1.html Code:

<asp:fileupload runat="server" id="uploadfile"/>         <asp:button runat="server" id="btnupload " onclick="btnupload_click " text=" upload "/>
HTML code

2. Background code:

 Public Partial classUpLoadFilesByStream:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { }        /// <summary>        ///Upload/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        protected voidBtnupload_click (Objectsender, EventArgs e) {            //for the file format and size of the judge, in the previous article has been involved, omitted here.             if( This. Uploadfile.hasfile) {//The Httppostedfile class provides access to individual files that the client has uploadedHttppostedfile HPF = This.                Uploadfile.postedfile; //httppostedfile HPF = request.files[0]; //file name                stringFileName =Path.getfilename (HPF.                FileName); //file size, per byte                intFilecontentlength =HPF.                ContentLength; //Upload Path                stringFilePath = Server.MapPath ("/files/"); //Binary Arrays                byte[] Filebytes =NULL; Filebytes=New byte[Filecontentlength]; //Create a Stream object and point to upload fileStream FileStream =HPF.                InputStream; //reads bytes from the current stream, reads into the byte arrayFileStream.Read (Filebytes,0, filecontentlength); //Full path (road strength + file name)                stringFullPath = FilePath +FileName; //Save to diskSavetodisk (filebytes, FullPath); }        }        /// <summary>        ///Save to disk/// </summary>        /// <param name= "bytes" >byte array</param>        /// <param name= "Savefullpath" >Full path</param>        /// <returns></returns>         Public voidSavetodisk (byte[] Bytes,stringSavefullpath) {            varFullPath =Path.getdirectoryname (Savefullpath); //If you do not have this folder, the new            if(!directory.exists (FullPath))            {directory.createdirectory (FullPath); }            //creates a file that returns a FileStream that provides read/write access to the file specified in path.             using(FileStream stream =file.create (Savefullpath)) {                //writes a byte array to a streamStream. Write (Bytes,0, Bytes.                Length); Stream.            Close (); }        }    }
. CS Code

Test pass!

C # upload file (ii) Saving a file using a binary stream

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.