在套用母版页的页面中应用input type="file" 上传图片
若是Request.Files 始终是0,添加以下信息:
(1)在套用页面Page_Load中添加 Form.Enctype = "multipart/form-data";不要在母版页中加
(2)确保input type="file"包含 name属性,不然也取不到上传的文件。
另附一段JS动态添加上传标签的JS代码:
1 function AddFileControl() { 2 var uploads = document.getElementsByName("upFile"); 3 if (uploads.length >= 10) { 4 alert("最多可以添加10个附件"); 5 return false; 6 } 7 var innerDiv = document.createElement("div"); 8 document.getElementById("dv1").appendChild(innerDiv); 9 var fileControl = document.createElement("input"); 10 fileControl.name = "upFile"; 11 fileControl.type = "file"; 12 innerDiv.appendChild(fileControl); 13 var btnControl = document.createElement("input"); 14 btnControl.name = "btnDelete"; 15 btnControl.type = "button"; 16 btnControl.setAttribute("value", "删除"); 17 btnControl.onclick = function () { DeleteFileControl(this.parentNode) }; 18 innerDiv.appendChild(btnControl); 19 } 20 21 function DeleteFileControl(obj) { 22 document.getElementById("dv1").removeChild(obj); 23 }
上传标签统一添加到页面名为:dv1的DIV标签中