Upload picture totals using asp.net

Source: Internet
Author: User

1 using standard HTML for image upload

Foreground code:

<body>
    <form id= "Form1" runat= "Server" >
    <div>
        <table>
            <tr>
                <TD colspan= "2" style= "height:21px" >
                    using standard HTML for image upload </td>
            </tr>
            <tr>
                <TD style= "width:400px" >
                    <input id= "Inputfile" style= "width:399px" type= "file" runat= "Server"/>< /td>
                <td style= "width:80px" >
                    <asp:button id= "Uploadbutton" runat= "server" text= "Upload picture" OnClick = "Uploadbutton_click"/></td>
            </tr>
            <tr>
                <td colspan= "2" >
                    <asp : Label id= "Lb_info" runat= "Server" forecolor= "Red" ></asp:Label></td>                
            </tr>
        </ table>    
    </div>
    </form>
</body>


Background code:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Using System.Web.UI.HtmlControls;

    Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) { } protected void Uploadbutton_click (object sender, EventArgs e) {string uploadname = inputfile.value;//Get
        The full path of the picture to upload, including filename//string uploadname = InputFile.PostedFile.FileName; String picturename = "";//uploaded picture name, with current time as filename, make sure file name does not duplicate if (inputfile.value!= "") {int idx = u
            Ploadname.lastindexof (".");
        string suffix = uploadname.substring (idx);//Get the suffix name of the uploaded picture Picturename = DateTime.Now.Ticks.ToString () + suffix; try {if (uploadname!= "") {string path = Server.mappa
          Th ("~/images/");      InputFile.PostedFile.SaveAs (path + picturename);
        } catch (Exception ex) {Response.Write (ex); }
    }
}


2 Single File upload

This is the most basic file upload, in the asp.net1.x does not have this FileUpload control, only HTML upload control, at that time to convert HTML controls to server controls, very difficult to use. In fact, all the beautiful effect of the file upload is derived from this FileUpload control, the first example, although simple is fundamental.

Foreground code:

<body> <form id= "Form1" runat= "Server" > <div> <table style= "width:90%" > <tr> <td style= "width:159px" colspan=2> <strong><span style= "
                Font-size:10pt "> Simplest single File upload </span></strong></td> </tr> <tr> &LT;TD style= "width:600px" > <asp:fileupload id= "FileUpload1" runat= "Server" width= "60 0px "/></td> <td align=left> <asp:button id=" Fileupload_button "Runa"
                t= "Server" text= "Upload picture" onclick= "Fileupload_button_click"/></td> </tr> <tr> &LT;TD colspan=2> <asp:label id= "Upload_info" runat= "Server" forecolor= "Red" Widt H= "767px" ></asp:Label></td> </tr> </table> </div> </fo Rm> </body>


Background code:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Using System.Web.UI.HtmlControls;

    Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) { } protected void Fileupload_button_click (object sender, EventArgs e) {try {if (Fileu Pload1. Postedfile.filename = = "")//if (Fileupload1.filename = = "")//if (! Fileupload1.hasfile)//Gets a value indicating whether the System.Web.UI.WebControls.FileUpload control contains files.
            Is true if the file is included, or false. {this. Upload_info. Text = "Please select Upload file."
            ";  else {string filepath = FileUpload1.PostedFile.FileName; Gets the full path to the file, including the file name, such as: C:/Documents and settings/administrator/my documents/my pictures/20022775_m.jpg//st Ring Filepath = Fileupload1.filename; Get uploaded filename 20022775_m.jpg string filename = filepath. Substring (filepath. LastIndexOf ("//") + 1);//20022775_m.jpg string serverpath = Server.MapPath ("~/images/") + filename;//obtain file in The location saved on the server c:/inetpub/wwwroot/website1/images/20022775_m.jpg FileUpload1.PostedFile.SaveAs (serverpath);//Will be uploaded Save the file as this. Upload_info. Text = "Upload succeeded."
            "; } catch (Exception ex) {this. Upload_info. Text = "An error occurred while uploading. The reason is: "+ ex."
        ToString (); }
    }
}


more than 3 file uploads

Foreground code:

<body> <form id= "Form1" runat= "Server" > <div> <table style= "width:343px" > <tr> <td style= "width:100px" > Multi-File Upload </td> &LT;TD St yle= "width:100px" > </td> </tr> <tr> <td s
                    tyle= "width:100px" > <asp:fileupload id= "FileUpload1" runat= "Server" width= "475px"/> </td> <td style= "width:100px" > </td> </ tr> <tr> <td style= "width:100px" > <asp:fileupload id= "F
                IleUpload2 "runat=" Server "width=" 475px "/></td> <td style=" width:100px ">
                    </td> </tr> <tr> <td style= "width:100px" > <asp:fileuploadId= "FileUpload3" runat= "Server" width= 475px "/></td> <td style=" width:100px ">
                    </td> </tr> <tr> <td style= "width:100px" > <asp:button id= "Bt_upload" runat= "Server" onclick= "Bt_upload_click" text= "Upload together"/> ; Asp:label id= "Lb_info" runat= "Server" forecolor= "Red" width= "448px" ></asp:Label></td> <
    TD style= "width:100px" > </td> </tr> </table> </div> </form> </body>


Background code:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Using System.Web.UI.HtmlControls;

    Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) { } protected void Bt_upload_click (object sender, EventArgs e) {if (FileUpload1.PostedFile.FileName = "" & amp;& FileUpload2.PostedFile.FileName = = "" && FileUpload3.PostedFile.FileName = "") {th Is.lb_info. Text = "Please select a file.
        ";
            else {httpfilecollection myfiles = request.files; for (int i = 0; i < myfiles. Count;
                i++) {Httppostedfile mypost = myfiles[i]; try {if (mypost). ContentLength > 0) {string filepath = MypoSt. Filename;//c:/documents and settings/administrator/my documents/my pictures/20022775_m.jpg string filename = filepath. Substring (filepath. LastIndexOf ("//") + 1);//20022775_m.jpg string serverpath = Server.MapPath ("~/images/") + filename ;//c:/inetpub/wwwroot/website2/images/20022775_m.jpg MyPost.
                        SaveAs (Serverpath); This.lb_info. Text = "Upload succeeded."
                    "; } catch (Exception ex) {this.lb_info. Text = "An error occurred while uploading. Reason: "+ ex."
                Message.tostring (); }
            }
        }
    }
}


4 Client Check upload file type (above picture for example)

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  


Note: Click Upload to trigger the client event first onclientclick= "Return Check_filetype ()"

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Using System.Web.UI.HtmlControls;

    Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) { } protected void Bt_upload_click (object sender, EventArgs e) {try {if (FileUpload1). Postedfile.filename = = "") {This.lb_info. Text = "Please select a file.
            ";
                else {string filepath = FileUpload1.PostedFile.FileName; if (! Isallowedextension (FILEUPLOAD1))//{//This.lb_info. Text = "The upload file is not in the correct format."
                "; } if (Isallowedextension (FileUpload1) = = true) {string filename = f Ilepath. Substring (filepath. LastIndexOf ("//") + 1);
                    String serverpath = Server.MapPath ("~/images/") + filename;
                    FileUpload1.PostedFile.SaveAs (Serverpath); This.lb_info. Text = "Upload succeeded."
                "; else {this.lb_info. Text = "Please upload the picture."
                "; The catch (Exception ex) {this.lb_info. Text = "An error occurred while uploading. Reason: "+ ex."
        ToString ();
        } private static bool Isallowedextension (FileUpload upfile) {string stroldfilepath = "";
        String strextension= "";
        String[] arrextension ={". gif", ". jpg", ". bmp", ". png"}; if (upfile. Postedfile.filename!= String. Empty) {Stroldfilepath = Upfile. postedfile.filename;//gets the full pathname of the file strextension = stroldfilepath.substring (Stroldfilepath.lastindexof (".")); /Obtain the file's extension, such as:. jpg for (int i = 0; i < arrextension.length; i++) {if StrexTension.
                Equals (Arrextension[i])) {return true;
    }} return false; Note: If you remove the client script and client event onclientclick= "return Check_filetype ()", in the background code//if (! Isallowedextension (FILEUPLOAD1))//{//This.lb_info. Text = "The upload file is not in the correct format."
                "; The IF (isallowedextension (FileUpload1) = True) is changed to: if (! Isallowedextension (FileUpload1)) {This.lb_info. Text = "The upload file is not in the correct format."
                "; else if (isallowedextension (FileUpload1) = = True) becomes the server-side check upload file type.


5 server-side check upload file type (file inside real format)

<body>
    <form id= "Form1" runat= "Server" >
    <div>
        <table>
            <tr>
                <TD colspan= "2" >
                    server Check upload file type </td>                
            </tr>
            <tr>
                <td style= "width:444px" >
                    <asp:fileupload id= "FileUpload1" runat= "Server" width= "432px"/></td> <td style=
                "Width : 80px ">
                    <asp:button id=" bt_upload "runat=" server "text=" Upload picture "onclick=" Bt_upload_click "/></td>
            </tr>
            <tr>
                <td colspan= "2" style= "height:21px" >
                    <asp:label id= "Lb_info" runat= "Server" forecolor= "Red" width= "515px" ></asp:Label></td>                
            </tr>
        </table >    
    </div>
    </form>
</body>


Background code:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Using System.IO;

    Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) { } protected void Bt_upload_click (object sender, EventArgs e) {try {if (FILEUPLOAD1.P Ostedfile.filename = = "") {This.lb_info. Text = "Please select a file.
            ";
                else {string filepath = FileUpload1.PostedFile.FileName; if (isallowedextension (FileUpload1) = = true) {string filename = filepath. Substring (filepath.
                    LastIndexOf ("//") + 1);
                    String serverpath = Server.MapPath ("images/") + filename; FileUpload1.PostedFile.SaveAs (SERVERPATH); This.lb_info. Text = "Upload succeeded."
                "; else {this.lb_info.
                Text = "please upload pictures"; A catch (Exception error) {This.lb_info. Text = "An error occurred while uploading. Reason: "+ error."
        ToString (); }} private static bool Isallowedextension (FileUpload upfile) {FileStream fs = new FileStream (upfile.
        Postedfile.filename, FileMode.Open, FileAccess.Read);
        BinaryReader r = new BinaryReader (FS);
        String fileclass = "";
        byte buffer;
            try {buffer = R.readbyte (); Fileclass = buffer.
            ToString ();
            Buffer = R.readbyte (); Fileclass + = buffer.
        ToString ();
        Catch {} r.close (); Fs.
        Close (); if (Fileclass = = "255216" | | fileclass = = "7173" | | fileclass== "6677" | | fileclass== "13780")//Description 255216 is jpg;7173 is gif;6677 is bmp,13780 is png;7790 is exe,8297 is rar {return true;
        else {return false;

 }
    }
}


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.