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