ASP. NET uploads image files

Source: Internet
Author: User
Using system;
Using system. IO; public partial class _ default: system. Web. UI. Page
{
Protected void btnupload_click (Object sender, eventargs E)
{
If (ispostback)
{
Boolean fileok = false; // defines the variable to determine whether the file is an image.
String Path = server. mappath ("~ /Uploadedimages/"); // specifies the file storage path if (fuupload. hasfile) // determines whether the control selects a file
{
String fileextension = path. getextension (fuupload. filename). tolower (); // use this method to get the extension of the uploaded file
String [] allowedextensions = {". GIF ",". PNG ",". JPEG ",". jpg "}; // defines the extended for (INT I = 0; I <allowedextensions. length; I ++)
{
If (fileextension = allowedextensions [I]) // If the uploaded file passes Detection
{
Fileok = true; // set this variable to true
}
}
} If (fileok) // determines whether the detection is successful
{
Try
{
If (! Directory. exists (PATH) // use the directory class method to determine whether the stored path directory exists
{
Directory. createdirectory (PATH); // create the directory if it does not exist
}
Fuupload. postedfile. saveas (path + fuupload. filename );
Label1.text = "File Uploaded! ";
}
Catch (exception ex)
{
Label1.text = "file cocould not be uploaded." + ex. message;
}
}
Else
{
Label1.text = "cannot accept files of this type .";
}
}
String strfilename = fuupload. filename;
}
} Html_code: <% @ 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"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: fileupload id = "fuupload" runat = "server" Height = "24px" style = "Z-index: 100;
Left: pixel PX; position: absolute; top: 84px "width =" 480px "/>
<Asp: Label id = "label1" runat = "server" style = "Z-index: 103; left: pixel; position: absolute;
Top: 52px "width =" 476px "> </ASP: Label>
<Asp: button id = "btnupload" runat = "server" Height = "24px" onclick = "btnupload_click"
Style = "Z-index: 102; left: pixel PX; position: absolute; top: 124px" text = "Upload" width = "84px"/>

</Div>
</Form>
</Body>
</Html>
Related Article

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.