asp.net mvc动态生成file控件批量上传文件
2011-01-08 16:08
666 查看
页面代码:
<body>
<div>
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<div id="FileList">
<div>
<input type="file" id="file0" name="file0" />
</div>
</div>
<p>
<a onclick="AddFile();" style="color: Blue; text-decoration: underline">新增文件</a></p>
<p>
<input type="submit" value="上传" /></p>
<%} %>
</div>
</body>
js代码:(根据用户自动生成上传控件)
var index = 1;
function AddFile() {
var ul = document.getElementById("FileList");
var inputDiv = document.createElement("div");
inputDiv.setAttribute("Id", "div" + index);
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("id", "file" + index);
file.setAttribute("name", "file" + index);
var btnDel = document.createElement("input");
btnDel.setAttribute("type", "button");
btnDel.setAttribute("value", "删除");
btnDel.setAttribute("Id", index);
btnDel.onclick = function() {
inputDiv.removeChild(file);
inputDiv.removeChild(btnDel);
ul.removeChild(inputDiv);
}
inputDiv.appendChild(file);
inputDiv.appendChild(btnDel);
ul.appendChild(inputDiv);
index++;
}
Controller的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase;
if (file==null&&file.ContentLength == 0)
continue;
//判断Upload文件夹是否存在,不存在就创建
string path = Server.MapPath("..//Upload");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path = AppDomain.CurrentDomain.BaseDirectory + "Upload/";
//获取上传的文件名
string fileName = Path.GetFileName(file.FileName);
//限制上传文件的类型
if (Path.GetExtension(fileName)!=".doc")
{
return Content("<script>alert('只能上传后缀名为.doc的文件');</script>");
}
//上传
file.SaveAs(Path.Combine(path,fileName));
}
return Content("<script>alert('上传文件成功');window.history.back();</script>");
}
<body>
<div>
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<div id="FileList">
<div>
<input type="file" id="file0" name="file0" />
</div>
</div>
<p>
<a onclick="AddFile();" style="color: Blue; text-decoration: underline">新增文件</a></p>
<p>
<input type="submit" value="上传" /></p>
<%} %>
</div>
</body>
js代码:(根据用户自动生成上传控件)
var index = 1;
function AddFile() {
var ul = document.getElementById("FileList");
var inputDiv = document.createElement("div");
inputDiv.setAttribute("Id", "div" + index);
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("id", "file" + index);
file.setAttribute("name", "file" + index);
var btnDel = document.createElement("input");
btnDel.setAttribute("type", "button");
btnDel.setAttribute("value", "删除");
btnDel.setAttribute("Id", index);
btnDel.onclick = function() {
inputDiv.removeChild(file);
inputDiv.removeChild(btnDel);
ul.removeChild(inputDiv);
}
inputDiv.appendChild(file);
inputDiv.appendChild(btnDel);
ul.appendChild(inputDiv);
index++;
}
Controller的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase;
if (file==null&&file.ContentLength == 0)
continue;
//判断Upload文件夹是否存在,不存在就创建
string path = Server.MapPath("..//Upload");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path = AppDomain.CurrentDomain.BaseDirectory + "Upload/";
//获取上传的文件名
string fileName = Path.GetFileName(file.FileName);
//限制上传文件的类型
if (Path.GetExtension(fileName)!=".doc")
{
return Content("<script>alert('只能上传后缀名为.doc的文件');</script>");
}
//上传
file.SaveAs(Path.Combine(path,fileName));
}
return Content("<script>alert('上传文件成功');window.history.back();</script>");
}
相关文章推荐
- asp.net mvc动态生成file控件批量上传文件 .
- ASP.NET MVC 4 批量上传文件
- SlickUploadFile控件 实现asp.net文件上传
- ASP.Net之HTMLInputFile控件上传文件
- asp.net html控件的File控件实现多文件上传实例分享
- asp.net上传文件使用fileupload控件,判断文件类型和大小,取得文件路径时报错【System.IO.FileNotFoundException:未能找到文件】的解决办法
- Flajaxian FileUploader——上传文件的asp.net控件
- ASP.NET MVC 4 批量上传文件
- Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件
- asp.net mvc 、 ajax 批量上传文件
- Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件
- asp.net 通过HtmlInputFile控件上传文件的类
- Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件
- ASP.NET 用.Net的File控件上传文件大文件上传之异常处理 的解决方案
- JQuery文件上传插件ajaxFileUpload在Asp.net MVC中的使用
- 用jQuery-File-Upload上传Excel文件(ASP.NET MVC)[附源码下载]
- JQuery文件上传插件ajaxFileUpload在Asp.net MVC中的使用
- asp.net mvc中读取input file上传的txt文件内容,但不需要把文件保存到服务器上
- ASP.net MVC FileUpload 文件上传
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法