多文件上传
页面部分:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MulitFilesUpload.aspx.cs" Inherits="WQT.WebUI.Jquery.MulitFilesUpload" %>
<!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>
<script type="text/javascript" src="../Lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
var fileIndex = 2;
var controlId = "file";
$(function() {
$("#btnAddNew").bind("click", function() {
$("#Container").append("<input type='file' name='"+controlId+fileIndex+"' />");
fileIndex++;
});
});
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="Container" style="width: 280px;">
<h3>多文件上传</h3>
<input type="file" name="file1" />
<a id="btnAddNew" href="#">添加项</a>
</div>
<div> <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />
</div>
</form>
</body>
</html>
后台:
namespace WQT.WebUI.Jquery{
public partial class MulitFilesUpload : System.Web.UI.Page {
private string _saveToPath = "~/Jquery/uploads/";
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
_saveToPath = Server.MapPath(_saveToPath);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
int fileCount = Request.Files.Count;
string fileName = string.Empty;
string filePath=string.Empty;
for (int i = 0; i < fileCount; i++)
{
fileName = Request.Files[i].FileName;
filePath = _saveToPath + fileName.Substring(fileName.LastIndexOf("\\")+1);
Request.Files[i].SaveAs(filePath) ;
}
}
}}
浙公网安备 33010602011771号