Source code of the File Uploaded By Asp.net

Source: Internet
Author: User
<% @ Page Language = "C #" src = "upload_cs.aspx.cs" autoeventwireup = "false" inherits = "howtos. multipleimageupdate. Upload" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title >::: i7land multi-File Upload program with no components: </title>
</Head>
<Body>
<Center>
<Form ID = "Upload" method = "Post" runat = "server" enctype = "multipart/form-Data">
<H3> multi-File Upload <HR> <br> to ensure the security of servers and websites, upload is prohibited. com or. DLL file, but allows you to upload any GIF, JPG, SwF, ASP, aspx, htm, HTML, JS, CSS, and other network files

<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <input type = "file" runat = "server" size = "50"> </P>
<P> <strong >:: </strong>
<Asp: linkbutton id = "linkbutton1" runat = "server" font-names = "verdana" font-bold = "true" font-size = "XX-small"> upload </ASP: linkbutton> <strong> ::
</Strong> <a href = "javascript: document. forms [0]. reset () "id =" linkbutton2 "style =" font-weight: bold; font-size: XX-small; font-family: verdana ">
Reset </a> <strong >:</strong> </P>
<P>
<Asp: label id = "label1" runat = "server" font-names = "verdana" font-bold = "true" font-size = "XX-small" width = "400px" borderstyle = "NONE" bordercolor = "white"> </ASP: label> </P>
<P> </P>
</Form>
</Center>
</Body>
</Html>

Code: This is (upload_cs.aspx.cs ):

Namespace howtos. multipleimageupdate
{
Public class upload: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. linkbutton linkbutton1;

# Region user defined code

Protected system. Web. UI. webcontrols. Label label1;

Private void page_load (system. Object sender, system. eventargs E)
{
If (this. ispostback)
This. saveimages ();
}

Private system. boolean saveimages (){
// Loop through the files uploaded

System. Web. httpfilecollection _ FILES = system. Web. httpcontext. Current. Request. files;

// Message to the user
System. Text. stringbuilder _ message = new system. Text. stringbuilder ";

Try
{
For (system. int32 _ ifile = 0; _ ifile <_ FILES. Count; _ ifile ++)
{

// Check to make sure the uploaded file is a jpg or GIF

System. Web. httppostedfile _ postedfile = _ FILES [_ ifile];
System. String _ filename, _ fileextension;

_ Filename = system. Io. Path. getfilename (
_ Postedfile. filename );

_ Fileextension = system. Io. Path. getextension (
_ Filename );

If (_ fileextension = ". BMP "| _ fileextension = ". BMP "| _ fileextension = ". GIF "| _ fileextension = ". GIF "| _ fileextension = ". SWF "| _ fileextension = ". SWF ")
{

// Save file to the proper directory
_ Postedfile. saveas (
System. Web. httpcontext. Current. Request. mappath (
"../Uploadfiles/images" + _ filename );
_ Message. append (_ filename + "<br> ";

}
Else if (_ fileextension = ". jpg" | _ fileextension = ". jpg"
{

// Save file to the proper directory
_ Postedfile. saveas (
System. Web. httpcontext. Current. Request. mappath (
"../Uploadfiles/images" + _ filename );
_ Message. append (_ filename + "<br> ";

}
Else if (_ fileextension = ". ASP "| _ fileextension = ". ASP "| _ fileextension = ". aspx "| _ fileextension = ". aspx "| _ fileextension = ". htm "| _ fileextension = ". htm "| _ fileextension = ". html "| _ fileextension = ". html "| _ fileextension = ". CS "| _ fileextension = ". CS "| _ fileextension = ". TXT "| _ fileextension = ". TXT "| _ fileextension = ". RAR "| _ fileextension = ". RAR "| _ fileextension = ". zip "| _ fileextension = ". zip "| _ fileextension = ". INC "| _ fileextension = ". INC "| _ fileextension = ". XML "| _ fileextension = ". XML "| _ fileextension = ". mdb "| _ fileextension = ". mdb "| _ fileextension = ". JS "| _ fileextension = ". JS "| _ fileextension = ". VB "| _ fileextension = ". VB "| _ fileextension = ". CSS "| _ fileextension = ". CSS "| _ fileextension = ". config "| _ fileextension = ". config "| _ fileextension = ". EXE "| _ fileextension = ". EXE"
{

// Save file to the proper directory
_ Postedfile. saveas (
System. Web. httpcontext. Current. Request. mappath (
"../Uploadfiles" + _ filename );
_ Message. append (_ filename + "<br> ";

}
Else if (_ fileextension = ". com "| _ fileextension = ". com "| _ fileextension = ". DLL "| _ fileextension = ". DLL"
Continue;
Else {

_ Message. append (_ filename + "<font color =" red "> Upload Failed. The file format may be incorrect! To ensure the security of websites and servers, this system prohibits the upload of any. com or. DLL files </font> <br> ";

}

}

Label1.text = _ message. tostring ();
Return true;
}
Catch (system. Exception ex)
{

Label1.text = ex. message;
Return false;

}

}
# Endregion

# Region web form designer generated code
Override protected void oninit (system. eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}

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.