. NET uploading a picture and turning it into a binary stream

Source: Internet
Author: User

Words don't say much, directly on the code

 <inputID= "Inputfile"style= "Width:399px"type= "File"runat= "Server" />        <Asp:buttonID= "Button1"runat= "Server"Text= "Save"onclick= "Button1_Click" />    <Asp:labelID= "Label1"runat= "Server"text= "Label"></Asp:label>        <Asp:imageID= "Image1"runat= "Server" />
 protected voidButton1_Click (Objectsender, EventArgs e) {            stringFileName =InputFile.PostedFile.FileName; Boolean FileOK=false; if(!string. Isnullorwhitespace (FileName)) {//Determine if the extension of the uploaded file is an allowed extension of ". gif", ". png", ". jpeg", ". jpg", ". BMP "String fileextension =System.IO.Path.GetExtension (fileName).                ToLower (); String[] Extensions= {". gif",". PNG",". JPEG",". jpg",". bmp" };  for(inti =0; i < extensions.length; i++)                {                    if(FileExtension = =Extensions[i]) {FileOK=true; }                }            }            //if the upload file name extension is allowed, save the file in the directory specified on the server            if(FileOK) {Try{httppostedfile Upfile= Inputfile.postedfile;//Httppostedfile object, which is used to read the properties of the uploaded image                    intFilelength = Upfile.contentlength;//length of the record file                    byte[] Filebytepicture =New byte[Filelength];//Initializes a byte array with the length of the picture to store the temporary picture fileStream fileStream = Upfile.inputstream;//creating a file stream objectFileStream.Read (Filebytepicture,0, filelength); stringFiletext =convert.tobase64string (filebytepicture); Label1.Text=Filetext; }                Catch(Exception ex) {MessageBox ("The file cannot be uploaded because:"+Ex.                Message); }            }            Else{MessageBox ("This type of file cannot be uploaded"); }        }        protected voidMessageBox (stringstr) {Page.ClientScript.RegisterStartupScript (Page.gettype (),"message","<script language= ' javascript ' Defer>alert ('"+ str +"');</script>"); }

. NET uploading a picture and turning it into a binary stream

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.