Asp. NET FileUpload File Upload Control Application Example _ Basic Application

Source: Internet
Author: User
Tags rar

Using the FileUpload control, you can provide a way for users to send files from a user's computer to a server. This control is useful when allowing users to upload pictures, text files, or other files. The files to be uploaded are submitted to the server as part of the browser request during the postback. After the file has been uploaded, you can manage the file in code.

To get a glimpse of FileUpload, let's take a look at some fileupload problems in practical applications.

1. Upload multiple files at a time

To upload multiple files at a time, we can handle each file separately like a flyer file, and in addition, we can use the Httpfilecollection class to capture all the files sent from the request object and then process each file separately, as follows:

Copy Code code as follows:

protected void Button1_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 occurred:" + ex.message;
}
}
}

2. Upload file Type verification

Verification of the upload file type can be done either on the client or on the server side. The client can use a validation control, but today we'll talk about how to authenticate on the server side. Top CS file has been used getextension to obtain the file extension, as long as a little judgment can be implemented to upload the type of verification:

Copy Code code as follows:

protected void Button1_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>" +
"FileName:" + System.IO.Path.GetFileName (fileupload1.filename) + "<br>" +
"File name 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 occurred:" + ex. Message.tostring ();
}
}
Else
{
Label1.Text = "only allowed to upload rar, zip file!" ";
}
}
Else
{
Label1.Text = "Did not select the file to upload!" ";
}
}

It is important to note that we cannot rely too much on the validation of client-side validation controls and the above methods on the server, because it is not difficult for users to simply change the file name extension to a permitted type to avoid the above validation.

3. Resolve File Size Limits

In asp.net 2.0 fileupload default upload file maximum 4M, but we can modify the relevant node in Web.cofig to change this default value, the relevant node is as follows:

Copy Code code as follows:

<system.web>
</system.web>

maxRequestLength represents the maximum number of files that can be uploaded, executiontimeout indicates how many seconds are allowed to occur before the ASP.net is closed.

4. "Multipart/form-data" and request coexist

Once you use the form upload file (the form's Enctype property value is Multipart/form-data) in an ASP program, the server can no longer use Request.Form to get the value of the form, which no longer exists in ASP.net 2.0:

Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
if (fileupload1.hasfile)
{
Try
{
Fileupload1.saveas (Server.MapPath ("upload") + "\" + fileupload1.filename);
Label1.Text = "Upload file:" + fileupload1.filename + "<br>" +
"Description:" + request.form["TextBox1"];//can also use "TextBox1.Text" to get instructions
}
catch (Exception ex)
{
Label1.Text = "Error occurred:" + ex. Message.tostring ();
}
}
Else
{
Label1.Text = "Did not select the file to upload!" ";
}
}

application Example

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 ">

 
 

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 Button1_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 (" 
 

This example applies the RegularExpressionValidator control limit can only upload Jpg, jpg, GIF, GIF format files, of course, the best backstage also do a limit, above has explained the specific operation method.

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.