C # ASP. NET FileUpload Detailed

Source: Internet
Author: User

Displays a text box control and a browse button that allows the user to select files to upload to the server. namespaces: System.Web.UI.WebControls
Assemblies: System.Web (in System.Web.dll)
Instance:
HTML code:

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Inputfile._default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Untitled Page </title>
<script language= "javascript" type= "Text/javascript" >
<! cdata[


]]>
</script>
<style type= "Text/css" >
. style1
{
Text-align:center;
}
#form1
{
Text-align:center;
}
</style>
<body>
<form id= "Form1" runat= "Server" method= "POST" enctype= "Multipart/form-data" >
<div style= "position:static;" >
<div class= "Style1" >
Presentation File Upload Control

</div>

<div class= "Style1" >

<asp:fileupload id= "File1" runat= "Server"/>
<asp:button id= "uploadbtn" runat= "Server" onclick= "button1_click" text= "Upload"/>
</div>
</div>
<asp:label id= "Label1" runat= "Server"
Width= "437px" height= "61px" ></asp:Label>
</form>
</body>

Background code:

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

Namespace Inputfile
{
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{

}

protected void Button1_Click (object sender, EventArgs e)
{//Get file information
string FileName = File1.PostedFile.FileName;
String file_str = "file name:" + FileName + "<br>";
file_str= "File type: +file1.postedfile.contenttype+" <br> ";
file_str= "File Length:" +file1.postedfile.contentlength.tostring () + "kb<br>";
Uploading files to the server
filename = filename.substring (filename.lastindexof ("\ \") + 1);//Remove the path to the filename (excluding the file name)

String upload_file = Server.MapPath ("./upload/") + filename;//Remove server virtual path, store upload file

File1.PostedFile.SaveAs (upload_file);//Start uploading files
Label1.Text =file_str+ "Upload file Success";
}
}
}

Common Properties:

(1) Fileupload1.hasfile is used to check whether the FileUpload has a specified file.

(2) HttpContext.Current.Request.MapPath ("~/") is to obtain the site of the disk absolute path, such as the D:\Inetpub\ServerControls\ path, the reason to do so, Because the FileUpload control must specify an absolute path, not a relative path, and the absolute path must also have write permissions.

(3) Fileupload1.saveas () is the way to store the uploaded file on disk.

(4) Fileupload1.filename is used to get the upload file name.

(5) FileUpload1.PostedFile.ContentLength is used to set or get the upload file size, in bytes.

(6) FileUpload1.PostedFile.ContentType to set or get the type of upload file
Instance:

Upload complete:

This article is from the "Shenzhou Dragon" blog, please be sure to keep this source http://shenzhoulong.blog.51cto.com/1191789/30313

C # ASP. NET FileUpload Detailed

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.