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