ASP. NET fileupload application instance

Source: Internet
Author: User

Default. aspx:

<% @ 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">
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
<Asp: regularexpressionvalidator id = "regularexpressionvalidator1" runat = "server" controltovalidate = "fileupload1"
Errormessage = "must be a jpg or GIF file" validationexpression = "^ ([A-Za-Z] :) | (// {2}/W +)/$ ?) (// (/W [/W]. * ))((.jpg |. jpg |. gif |. GIF) {$ Article $} quot;> </ASP: regularexpressionvalidator>
</Form>

</Body>
</Html>

 

Default. aspx. CS:

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 button#click (Object sender, eventargs E)
{
String savepath = @ "F:/111 /";
If (fileupload1.hasfile)
{
String filename;
Filename = fileupload1.filename;
Savepath + = filename;
Fileupload1.saveas (savepath );
Page. response. Write (fileupload1.postedfile. contenttype + fileupload1.postedfile. contentlength + "<br> ");
Page. response. Write (" ");

}
Else
{
Page. response. Write ("fff ");
}
}

}

Remove the green part to upload any file. It uses a regular expression to verify the type of the file to be uploaded.

 

Example 2:

 

Using the fileupload Server Control in ASP. NET 2.0 can easily upload files to the server. A simple example is as follows:

Aspx:

Program code
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "fileupload. aspx. cs" inherits = "fileupload" %>

<〈! 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> fileupload File Upload example -mzwu.com </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" _ disibledevent = "button#click" text = "Upload File"/> <br/>
<Asp: Label id = "label1" runat = "server" Height = "269px" text = "label" width = "360px"> </ASP: Label> </div>
</Form>
</Body>
</Html>

Aspx. CS:

Program code
Protected void button#click (Object sender, eventargs E)
{
If (fileupload1.hasfile)
{
Try
{
Fileupload1.saveas (server. mappath ("Upload") + "//" + fileupload1.filename );
Label1.text = "client path:" + fileupload1.postedfile. filename + "<br>" +
"File name:" + system. Io. Path. getfilename (fileupload1.filename) + "<br>" +
"File Extension:" + system. Io. Path. getextension (fileupload1.filename) + "<br>" +
"File size:" + fileupload1.postedfile. contentlength + "kb <br>" +
"File MIME type:" + fileupload1.postedfile. contenttype + "<br>" +
"Save path:" + server. mappath ("Upload") + "//" + fileupload1.filename;
}
Catch (exception ex)
{
Label1.text = "error:" + ex. Message. tostring ();
}
}
Else
{
Label1.text = "no file to upload is selected! ";
}

}

1. upload multiple files at a time

To upload multiple files at a time, we can process each file as a leaflet file. In addition, we can also use the httpfilecollection class to capture all the files sent from the request object, then process each file separately. The Code is as follows:

Aspx. CS:

Program code
Protected void button#click (Object sender, eventargs E)
{
String filepath = server. mappath ("Upload") + "//";
Httpfilecollection uploadfiles = request. files;
For (INT I = 0; I <uploadfiles. Count; I ++)
{
Httppostedfile postedfile = uploadfiles [I];
Try
{
If (postedfile. contentlength> 0)
{
Label1.text + = "file #" + (I + 1) + ":" + system. Io. Path. getfilename (postedfile. filename) + "<br/> ";
Postedfile. saveas (filepath + system. Io. Path. getfilename (postedfile. filename ));
}
}
Catch (exception ex)
{
Label1.text + = "error:" + ex. message;
}
}
}

2. Verify the Upload File Type

You can verify the file type on either the client or the server. The client can use the verification control, but today we mainly talk about how to verify on the server. In the above CS file, the file extension has been obtained using getextension. You only need to make a slight judgment to verify the upload type:

Aspx. CS:

Program code
Protected void button#click (Object sender, eventargs E)
{
If (fileupload1.hasfile)
{
Fileext = system. Io. Path. getextension (fileupload1.filename );
If (fileext = ". rar" | fileext = ". Zip ")
{
Try
{
Fileupload1.saveas (server. mappath ("Upload") + "//" + fileupload1.filename );
Label1.text = "client path:" + fileupload1.postedfile. filename + "<br>" +
"File name:" + system. Io. Path. getfilename (fileupload1.filename) + "<br>" +
"File Extension:" + system. Io. Path. getextension (fileupload1.filename) + "<br>" +
"File size:" + fileupload1.postedfile. contentlength + "kb <br>" +
"File MIME type:" + fileupload1.postedfile. contenttype + "<br>" +
"Save path:" + server. mappath ("Upload") + "//" + fileupload1.filename;
}
Catch (exception ex)
{
Label1.text = "error:" + ex. Message. tostring ();
}
}
Else
{
Label1.text = "only RAR and ZIP files can be uploaded! ";
}
}
Else
{
Label1.text = "no file to upload is selected! ";
}

}

It should be noted that we should not rely too much on the client-side verification control and the server-side verification method, because users only need to change the file extension to the allowed type to avoid the above verification, this is not difficult for users.

3. Solve the file size limit

In ASP. NET 2.0, the maximum size of the file to be uploaded is 4 MB by default. However, you can modify the default value in Web. cofig. The related nodes are as follows:

Program code
<System. Web>
<Httpruntime maxrequestlength = "40690" executiontimeout = "6000"/>
</System. Web>

Maxrequestlength indicates the maximum value of a file to be uploaded, and executiontimeout indicates the number of uploads allowed before ASP. NET is disabled.

4. "multipart/form-Data" and request coexist

In ASP, once a file is uploaded using a form (the value of form's enctype is multipart/form-data), the server cannot use request. form to obtain the value of the form. NET 2.0 does not exist anymore:

Aspx. CS:

Program code
Protected void button#click (Object sender, eventargs E)
{
If (fileupload1.hasfile)
{
Try
{
Fileupload1.saveas (server. mappath ("Upload") + "//" + fileupload1.filename );
Label1.text = "Upload File:" + fileupload1.filename + "<br>" +
"NOTE:" + request. Form ["textbox1"]; // you can also use "textbox1.text" to obtain the description.
}
Catch (exception ex)
{
Label1.text = "error:" + ex. Message. tostring ();
}
}
Else
{
Label1.text = "no file to upload is selected! ";
}
}

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.