asp.net more than 2 files upload small experience

Source: Internet
Author: User
Tags file upload httpcontext reset
Asp.net| Upload

To achieve any number of file upload functions, click a button to add a File upload box, previously seen on the network hard disk. JavaScript I know how to implement any add upload file control, the problem is to add HTML control, I do not know how to get the server side can get the file.

So on Google search "asp.net multiple file Upload", and really found a file, labeled "in asp.net to achieve multiple file upload", The article inside is vb.net implementation, function and I want exactly the same, I mainly want to see how the server side to get the client upload file, read the text of the code, originally so simple, System.Web.HttpContext.Current.Request.Files contains the client browser upload text piece, I used C # to write a simple code, originally thought should be able to, the result unexpectedly upload 3 pictures System.Web.HttpContext.Current.Request.Files return the file format or 0.

Don't know why, look at the code, so simple can not be wrong ah, and then look at the Google search results in a few other articles, found that I read the first article is not the original, the author's site on the original instance has two versions, one is vb.net one is C #, now I do not have to write their own, Copy the original code to the local, run, really can ah, then I write code how not? Repeatedly compared to my code and the article in the difference between the code, tried a few places, and finally found no relationship with other places, the reason for the <form id= "Form1" runat= "Server" enctype= "Multipart/form-data" > On the Enctype property, the VS 2005 page does not have this attribute, and in the article example, I later added enctype= "Multipart/form-data" After the System.Web.HttpContext.Current.Request.Files can z normal access to the number of files.

Probably the VS 2003 page default has this attribute, otherwise such important attribute authors should be mentioned in the article.

Reference:
"Multi-File upload in asp.net"

I did the test code:

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

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script language= "JavaScript" >
function AddFile ()
{
var str = ' <input type= "file" size= "name=" file ">"
document.getElementById (' MyFile '). insertAdjacentHTML ("BeforeEnd", str)
}
</script>
<title>untitled page</title>
<body>
<form id= "Form1" runat= "Server" enctype= "Multipart/form-data" >
<input type= "button" value= "Add" >
<input type= "button" value= "Reset (reset)" >
<asp:button runat= "Server" text= "upload" id= "Upload" ></asp:Button>
<div id= "MyFile" >
<input type= "File" name= "file"/>
</div>
</form>
</body>

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
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;

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

Httpfilecollection _files = System.Web.HttpContext.Current.Request.Files;

for (int i = 0; i < _files. Count; i++)
{
_files[i]. SaveAs (Server.MapPath ("~/files/" + _files[i). FileName));
}
}
}
}



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.