適用IE
client側
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無題のページ</title>
<%--<style>
input{border:1px solid #333333;color:#666666;background:#eeeeee;font:normal 12px Tahoma;height:18px}
</style>--%>
<script type="text/javascript">
var num = 0;
function addFile(idText, idFile) {
document.getElementById(idText).value = document.getElementById(idFile).value;
if(num == 0){
document.getElementById("filename").style.border = "0";
document.getElementById("button").style.display = "none";
}
if (num != 0) {
var x = "file" + (num - 1).toString();
if (idFile != x) {
return;
}
}
if (num > 0) {
var _idtext = "text" + (num - 1).toString();
var _idbutton = "button" + (num - 1).toString();
document.getElementById(_idtext).style.border = "0";
document.getElementById(_idbutton).style.display = "none";
}
var div = document.createElement("div");
var idtext = "text" + num.toString();
var t = document.createElement("input");
t.setAttribute("type", "text");
t.setAttribute("size", "50");
t.setAttribute("id", idtext);
div.appendChild(t);
var idbutton = "button" + num.toString();
var b = document.createElement("input");
b.setAttribute("type", "button");
b.setAttribute("id", idbutton);
b.setAttribute("value", "参照..");
div.appendChild(b);
var idfile = "file" + num.toString();
var f = document.createElement("input");
f.setAttribute("type", "file");
f.setAttribute("name", "file");
f.setAttribute("size", "50");
f.setAttribute("id", idfile);
div.appendChild(f);
document.getElementById("_container").appendChild(div);
document.getElementById(idfile).onchange = function() { addFile(idtext, idfile) };
document.getElementById(idfile).style.position = "absolute";
document.getElementById(idfile).style.filter = "alpha(opacity=0)";
document.getElementById(idfile).style.width = "30px";
document.getElementById(idbutton).style.position = "absolute";
document.getElementById(idbutton).onmouseover = function() { fclick(document.getElementById(idfile)) };
num = num + 1;
}
function deteFile(o) {
while (o.tagName != "div") o = o.parentNode;
o.parentNode.removeChild(o);
}
function fclick(obj) {
with (obj) {
style.posTop = event.srcElement.offsetTop;
var x = event.x - offsetWidth / 2;
if (x < event.srcElement.offsetLeft) x = event.srcElement.offsetLeft;
if (x > event.srcElement.offsetLeft + event.srcElement.offsetWidth - offsetWidth) x = event.srcElement.offsetLeft + event.srcElement.offsetWidth - offsetWidth;
style.posLeft = x;
}
}
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<h3>
MultiFile Upload</h3>
User Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div id="_container">
<input type="text" size="50" id="filename" />
<input type="button" name="button" id="button" style="position:absolute" value="参照.." οnmοuseοver="fclick(t_file)" />
<input name="upload" type="file" style="position:absolute;filter:alpha(opacity=0);width:30px; left: 372px; top: 85px;"
id="t_file" οnchange="addFile('filename', 't_file');" hidefocus"/>
</div>
<div style="padding: 10px 0">
<asp:Button runat="server" Text="Upload" ID="UploadButton" OnClick=" UploadButton_Click">
</asp:Button>
</div>
<div>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</div>
</form>
</body>
</html>
server側
protected void UploadButton_Click(object sender, EventArgs e)
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您输入的用户名是:" + TextBox1.Text + "<br/>");
strMsg.Append("The files you uploaded are:<hr color='red'/>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("Type:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("Client Adress:" + postedFile.FileName + "<br>");
strMsg.Append("File Name:" + fileName + "<br>");
strMsg.Append("File Extention:" + fileExtension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("Upoladfiles/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}