使用:把控件拖到页面中,设置SavePath路径即可上传,如果还要扩展功能请继承此控件。。。
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class MutiFileUpload : System.Web.UI.UserControl
{
public string SavePath
{
get
{
if (ViewState["SavePath"] == null)
{
ViewState["SavePath"] = Environment.CurrentDirectory;
}
return ViewState["SavePath"].ToString();
}
set
{
if (!Object.Equals(ViewState["SavePath"] , value))
{
ViewState["SavePath"] = value;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(this.Page.Form!=null)
this.Page.Form.Method = "post";
this.Page.Form.Enctype = "multipart/form-data";
}
int count = Request.Files.Count;
for (int i = 0; i < count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file == null || file.ContentLength <= 0) continue;
file.SaveAs(Path.Combine(SavePath, Path.GetFileName(file.FileName)));
}
}
}
}
前台代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MutiFileUpload.ascx.cs" Inherits="WebApplication1.MutiFileUpload" %>
<script type="text/javascript">
function AddFileUpload(o) {
var panel = document.getElementById("fileUploads");
var div = document.createElement("div");
div.innerHtml = "<br/>";
panel.appendChild(div);
var fileUpload = document.createElement("input");
fileUpload.setAttribute("type", "file");
fileUpload.setAttribute("name", Date().toString());
fileUpload.setAttribute("id", Date().toString());
fileUpload.onchange = function() { AddFileUpload(fileUpload); };
if (o.value != null || o.value != "") {
panel.appendChild(fileUpload);
var btnAdd = document.createElement("input");
btnAdd.value = "X";
btnAdd.setAttribute("type", "button");
btnAdd.setAttribute("id", Date().toString());
btnAdd.onclick = function() { RemoveFileUploadByObject(btnAdd, fileUpload); };
panel.appendChild(btnAdd);
}
return fileUpload;
}
function RemoveFileUploadByID(buttonID, fileUploadID) {
var panel = document.getElementById("fileUploads");
var fileUpload = document.getElementById(fileUploadID);
var button = document.getElementById(buttonID);
RemoveFileUploadByObject(button, fileUpload);
}
function RemoveFileUploadByObject(button,fileUpload) {
var panel = document.getElementById("fileUploads");
panel.removeChild(button);
panel.removeChild(fileUpload);
}
function submitFunc() {
document.forms.item(0).submit();
}
window.onload = function() {
// var fileInit = this.parent.document.createElement("input");
// fileInit.setAttribute("type", "file");
// fileInit.setAttribute("id", "initFile");
// fileInit.setAttribute("display", "none");
// this.parent.document.forms.item(0).appendChild(fileInit);
var container = this.document.getElementById("fileUploads");
container.onmousemove = function() {
this.style.cursor = "progress";
}
}
</script>
<p id="fileUploads">
<input type="text" id="AddingFiles" value="AddingFiles"
style=" padding-right:1px; 75px;" readonly="readonly" /><input id="Button1" type="button" value="AddFiles" onclick="AddFileUpload(this);" /></p>
<p>
<input id="Button2" type="submit" value="upload" /></p>