Directly read the contents of the uploaded file without saving

Source: Internet
Author: User

The code is very simple, do not be wordy.

C#:

<%@ Page language=" c#" autoeventwireup="true" %> <! DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Btnupload_click (object sender, EventArgs e) {if (FileUpload1.PostedFile.InputStream.Length < 1) { Msg.text = " Please select file. "; Return string FileName = Fileupload1.filename; if (Filename.tolower (). IndexOf (" txt" = = = 1) {msg.text = " Please select a text file. "; Return int FileLen = FileUpload1.PostedFile.ContentLength; byte[] input = new Byte[filelen]; System.IO.Stream uploadstream = FileUpload1.PostedFile.InputStream; Uploadstream.read (input, 0, FileLen); uploadstream.position = 0; System.IO.StreamReader sr = new System.IO.StreamReader (Uploadstream, System.Text.Encoding.Default); Msg.text = " you uploadThe file content is:<br/><br/>" + Sr. ReadToEnd (); Sr. Close (); Uploadstream.close (); Uploadstream = null; sr = null; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title> directly read the contents of uploaded files without saving </title> </head> <body> <form id=" mengxianhui" Runat="server"> <asp:fileupload id=" fileupload1" runat="server" /> <asp:button id="btnupload" runat="server" text=" Upload " Onclick="btnupload_click"></asp:button> <p> <asp:label id=" msg" runat="server" forecolor=" Red"></asp:label></p> </form> </body&aMP;GT; </html>

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.