Use of HTML control File in ASP.

Source: Internet
Author: User
Tags html form

1. Foreground code

<title></title>
<link href= ". /.. /.. /stylesheets/bootstrap-theme.css "rel=" stylesheet "type=" Text/css "/>
<link href= ". /.. /.. /stylesheets/bootstrap.css "rel=" stylesheet "type=" Text/css "/>
<script src= ". /.. /.. /javascripts/bootstrap.js "type=" Text/javascript "></script>
<script src= ". /.. /.. /javascripts/common-validate.js "type=" Text/javascript "></script>

<script type= "Text/javascript" >
function Checkfile () {
var file = document.getelementsbyname ("file") [0].value;
if (file.trim () = = "") {
Alert ("Please select File!! ");
return false;
}
if (!isexcelxls (file)) {
Alert ("Excel file must be. xls format!! ");
return false;
}
}
</script>

<body>
<form id= "Form1" runat= "Server"enctype= "Multipart/form-data"> <!--red part cannot be less---
<div class= "Panel Panel-primary" >
<div class= "Panel-heading" >

</div>
<div class= "Panel-body" style= "margin-top:20px;" >
<div class= "Form-group" >
<div style= "float:left;width:240px;padding-top:5px;text-align:right;padding-right:10px;" >
<label class= "Control-label" > Please select File </label>
</div>
<div style= "float:left;width:300px;padding-top:5px" >
<input type= "File" name= "file"/>
</div>
<div style= "Float:left;width:120px;text-align:right" >
<asp:button id= "Btn_export" runat= "Server" text= "Export packing slip" cssclass= "btn btn-info" onclientclick= "return Checkfile ()"
onclick= "Btn_export_click"/>
</div>
<div style= "FLOAT:LEFT;WIDTH:120PX;TEXT-ALIGN:RIGHT;PADDING-TOP:6PX;" >
<a href= ". /.. /excel/template/export-zhuangxiang-template.xls "> Templates </a>
</div>
</div>
</div>
<div class= "Panel-footer" >
</div>
</div>
</form>
</body>

2. Get the code for the file in the background

protected void Btn_export_click (object sender, EventArgs e)
{
httpfilecollection files = System.Web.HttpContext.Current.Request.Files;
int count = files. Count;
String name = Files[0]. FileName;

String fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + name; File name, current time (YYYYMMDDHHMMSS)
String webfilepath = Server.MapPath ("~/viewpage/excel/zhuangxiang/") + fileName; Server-side file path
Files[0]. SaveAs (Webfilepath);

DataTable table = new Dbconn (). Getexcelcontent (Webfilepath);

foreach (DataRow dr in table. Rows)
{
if (Dr[0]. ToString ()! = "" && dr[1]. ToString ()! = "")
{

}
}


}

3. The enctype attribute must be added

enctype   properties have three values:

1, application/ x-www-form-urlencoded
2, Multipart/form-data
3, Text/plain
where application/x-www-form-urlencoded is the default value, The function is to set the encoding of the form transfer. For example, we have seen Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded") in Ajax; But in HTML form table dropdowns can not write enctype=application/x-www-form-urlencoded, because the default HTML form is this type of transmission encoding.
and Multipart/form-data is used to develop a special type of transmission data, mainly we upload non-text content, compared to tablets or MP3 and so on.
Text/plain is the meaning of plain text transmission, when sending the message to set this type of encoding, otherwise there will be the problem of encoding confusion when receiving. Network often take text/plain and text/html to do comparison, in fact, these two very good distinction, the former is used to transfer plain text files, the latter is the transmission of HTML code encoding type, in the sending of the header file is used. ① and ③ can not be used to upload files, only multipart/form-data to complete the file data

mentioned above the mime, its English name is "multipurpose Internet Mail Extensions" The Multifunctional Internet Mail expansion service, a multipurpose e-mail Expansion protocol, was first applied to the email system in 1992, but was later applied to the browser. The server will tell the browser the type of multimedia data that they send, and the notification means that it describes the MIME type of the multimedia data, allowing the browser to know what the MP3 file is, which is the shockwave file, and so on. The server puts the MIME marker in the transferred data to tell the browser which plug-in to use to read the relevant file.

Use of HTML control File in ASP.

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.