首页 > Web开发 > 详细

ASP.NET 使用HTML file控件进行文件上传

时间:2014-04-02 09:45:06      阅读:580      评论:0      收藏:0      [点我收藏+]

第一种:

bubuko.com,布布扣
1  <form id="form1"enctype="multipart/form-data" action="Default.aspx" method="post" >
2  <input type="button" value="添加附件" onclick="addAttachment()" />  
3     <div id="divAttachment">  
4     </div>  
5    <input type="submit"  value="提交"/>
6 </form>
bubuko.com,布布扣

JS:

bubuko.com,布布扣
 1       <script language="javascript">
 2     function addAttachment() {  
 3             var div = document.createElement("div");  
 4             var input = document.createElement("input");  
 5             input.setAttribute("type", "file");  
 6             input.setAttribute("name", "file");  
 7             input.setAttribute("runat", "server");
 8             input.setAttribute("size", "50");  
 9             div.appendChild(input);  
10          
11             var btnRemove = document.createElement("a");  
12             btnRemove.setAttribute("href", "#");  
13             btnRemove.innerText = "删除";  
14             btnRemove.setAttribute("onclick", "removeAttachment(this)");    
15             btnRemove.setAttribute("style", "text-decoration: none");  
16             var span = document.createElement("span");  
17             span.setAttribute("style", "padding-left: 5px");  
18             span.appendChild(btnRemove);  
19             div.appendChild(span);  
20             document.getElementById("divAttachment").appendChild(div);  
21         }  
22         function removeAttachment(ctrl) {  
23             while (ctrl.tagName != "DIV") {  
24            
25                 ctrl = ctrl.parentNode;  
26             }  
27             ctrl.parentNode.removeChild(ctrl);  
28         }  
29       
30  </script>
bubuko.com,布布扣

后台代码:

bubuko.com,布布扣
 1  protected void Page_Load(object sender, EventArgs e)
 2     {
 3 
 4         if (!Page.IsPostBack) {
 5             System.Web.HttpFileCollection files = Request.Files;
 6 
 7             for (int fileCount = 0; fileCount < files.Count; fileCount++)
 8             {
 9 
10                 System.Web.HttpPostedFile postedfile = files[fileCount];
11                 int size = postedfile.ContentLength;
12                 if (size <= 8388608)
13                 {
14                     string fileName = System.IO.Path.GetFileName(postedfile.FileName);
15                     if (!String.IsNullOrEmpty(fileName))
16                     {
17 
18                         string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型  
19 
20                         //上传目录  
21                         string directory = Server.MapPath("upload");
22                         //文件全路径  
23                         string path = directory + "\\" + fileName;
24 
25                         //判断目录是否存在  
26                         if (!Directory.Exists(directory))
27                         {
28                             Directory.CreateDirectory(directory);
29                         }
30                         //文件存在就删除文件  
31                         if (File.Exists(path))
32                         {
33                             File.Delete(path);
34                         }
35                         //上传到服务器的路径  
36 
37                         postedfile.SaveAs(path);
38                     }
39                 }
40                 else if (size > 8388608)
41                 {
42                     Response.Write("<script>alert(‘上传文件不得超过8M‘);</script>");
43 
44                 }
45             }  
46         
47         }
48    }
bubuko.com,布布扣

第二种:

使用asp.net的服务器控件 BUTTON按钮进行提交

bubuko.com,布布扣
<form id="form1" runat="server"  enctype="multipart/form-data" action="Default.aspx" method="post" >

 <input type="button" value="添加附件" onclick="addAttachment()" />  
 <div id="divAttachment">  
 </div>  
 <asp:Button runat="server" ID="button1" Text="提交" OnClick="sub_Fun" />
 </form>
bubuko.com,布布扣

上面JS中去掉

input.setAttribute("runat", "server");
后台代码添加一个 public void sub_Fun(object sender, EventArgs e){}函数即可。

ASP.NET 使用HTML file控件进行文件上传,布布扣,bubuko.com

ASP.NET 使用HTML file控件进行文件上传

原文:http://www.cnblogs.com/eason-chan/p/3638488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!