ASP.NET 使用HTML file控件进行文件上传
2014-04-01 15:57
405 查看
第一种:
JS:
后台代码:
第二种:
使用asp.net的服务器控件 BUTTON按钮进行提交
上面JS中去掉
<form id="form1"enctype="multipart/form-data" action="Default.aspx" method="post" > <input type="button" value="添加附件" onclick="addAttachment()" /> <div id="divAttachment"> </div> <input type="submit" value="提交"/> </form>
JS:
<script language="javascript"> function addAttachment() { var div = document.createElement("div"); var input = document.createElement("input"); input.setAttribute("type", "file"); input.setAttribute("name", "file"); input.setAttribute("runat", "server"); input.setAttribute("size", "50"); div.appendChild(input); var btnRemove = document.createElement("a"); btnRemove.setAttribute("href", "#"); btnRemove.innerText = "删除"; btnRemove.setAttribute("onclick", "removeAttachment(this)"); btnRemove.setAttribute("style", "text-decoration: none"); var span = document.createElement("span"); span.setAttribute("style", "padding-left: 5px"); span.appendChild(btnRemove); div.appendChild(span); document.getElementById("divAttachment").appendChild(div); } function removeAttachment(ctrl) { while (ctrl.tagName != "DIV") { ctrl = ctrl.parentNode; } ctrl.parentNode.removeChild(ctrl); } </script>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { System.Web.HttpFileCollection files = Request.Files; for (int fileCount = 0; fileCount < files.Count; fileCount++) { System.Web.HttpPostedFile postedfile = files[fileCount]; int size = postedfile.ContentLength; if (size <= 8388608) { string fileName = System.IO.Path.GetFileName(postedfile.FileName); if (!String.IsNullOrEmpty(fileName)) { string fileExtension = System.IO.Path.GetExtension(fileName); //获取文件类型 //上传目录 string directory = Server.MapPath("upload"); //文件全路径 string path = directory + "\\" + fileName; //判断目录是否存在 if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //文件存在就删除文件 if (File.Exists(path)) { File.Delete(path); } //上传到服务器的路径 postedfile.SaveAs(path); } } else if (size > 8388608) { Response.Write("<script>alert('上传文件不得超过8M');</script>"); } } } }
第二种:
使用asp.net的服务器控件 BUTTON按钮进行提交
<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>
上面JS中去掉
input.setAttribute("runat", "server"); 后台代码添加一个 public void sub_Fun(object sender, EventArgs e){}函数即可。
相关文章推荐
- ASP.NET中采用HTML控件File进行多文件上传
- asp.net文件上传用到html的file控件,在使用母版页Master,更改form的Enctype=multipart/form-data的方法
- Asp.NET使用HTML控件上传文件
- ASP.NET同时上传多个文件,和不使用HTMLFileInput上传的方法(resource about file upload)
- asp.net,C#,html控件的File控件文件上传简单实例,vs2010
- ASP.Net之HTMLInputFile控件上传文件
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- asp.net上传文件使用fileupload控件,判断文件类型和大小,取得文件路径时报错【System.IO.FileNotFoundException:未能找到文件】的解决办法
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- asp.net上传文件使用file控件,判断文件大小,取得文件路径时报错【System.IO.FileNotFoundException:未能找到文件】的解决办法
- Asp.NET使用HTML控件上传文件
- asp.net html控件的File控件实现多文件上传实例分享
- asp.net html控件的File控件实现多文件上传实例分享
- asp.net 通过HtmlInputFile控件上传文件的类
- asp.net 通过HtmlInputFile控件上传文件的类
- [导入]在asp.net中利用FileUplad控件从同一个页面上传多个文件
- Asp.net_使用FileUpload控件上传文件通用方法分享
- Flajaxian FileUploader——上传文件的asp.net控件
- asp.net mvc动态生成file控件批量上传文件
- Asp.net 使用Neatupload 第三方控件上传大文件,在IIS7上无法正常工作解决