If you want to realize the function of uploading any number of files, click a button to add a file upload box, which you have seen on the network hard drive before. I know how to add any upload file control using JavaScript. The problem is that the Html control is added, and I don’t know how to make the server side obtain the file.
So I searched Google for "ASP.NET multiple file upload", and I actually found an article titled "Implementing multiple file upload in ASP.NET". The article was implemented in VB.NET, and the function was exactly the same as what I wanted. I mainly want to see how the server obtains the files uploaded by the client. After reading the code in the article, it turns out that it is so simple. System.Web.HttpContext.Current.Request.Files contains the files uploaded by the client browser. I use C# I wrote a simple code, originally thinking it should be OK, but unexpectedly, after uploading 3 images, System.Web.HttpContext.Current.Request.Files returned 0 file formats.
I don’t know why, but I looked at the code. It’s so simple and it can’t be wrong. Then I looked at several other articles in the Google search results. I found that the first article I looked at was not the original work. There are two examples of the original work on the author’s website. There are two versions, one is VB.NET and the other is C#. Now I don’t have to write it myself. I copy the original code to the local and run it. It works, so why doesn’t the code I wrote work? I repeatedly compared the difference between my code and the code in the article, tried several places, and finally found that it had nothing to do with other places. The reason was <form id="form1" runat="server" enctype="multipart/form- data"> enctype attribute, the page created in VS 2005 does not have this attribute, but the article example does. I later added enctype="multipart/form-data" and System.Web.HttpContext.Current.Request.Files became The number of files can be obtained normally.
Probably the pages built in VS 2003 have this attribute by default, otherwise the author of such an important attribute would have mentioned it in the article.
refer to:
"Implementing multiple file uploads in ASP.NET"
The test code I made:
<%@ 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="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
<html xmlns=" http://www.w3.org/1999/xhtml " >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="button" value="Add" onclick="addFile()">
<input onclick="this.form.reset()" type="button" value="Reset(ReSet)">
<asp:Button Runat="server" Text="Upload" ID="Upload" OnClick="Upload_Click1" ></asp:Button>
<div id="MyFile">
<input type="file" name="File" />
</div>
</form>
</body>
</html>
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));
}
}
}
}
http://bg5sbk.cnblogs.com/archive/2006/06/11/mulitfileuploadtest.html