文件上传

需用到控件FileUpload

复制代码
string[] sl = FileUpload1.FileName.Split('.');
if (sl[sl.Length - 1] == "aspx" || sl[sl.Length - 1] == "asp")   //保证文件安全 约束可上传类型
{   
  Response.Write("<script>alert('不可上传')</script>");   
  return;
}
FileUpload1.SaveAs(Server.MapPath("ul/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName));
//SaveAs保存文件 ,Server.MapPath返回绝对路径,文件后缀以及防止重名,ul为文件夹名称
复制代码

 accept=".txt,.png"    ----设置用户可选择的文件格式;

允许上传的最大文件大小4M,可以在配置文件web.config中扩容: <httpRuntime maxRequestLength="409600" />

在JS中写限制:

document.getElementById("Button1").onclick = function () {                                   
        if (document.getElementById("FileUpload1").files[0].size > 1024 * 1024 * 4) {
            alert("文件太大!");
            return false;
        }
    }
原文地址:https://www.cnblogs.com/gbbwzz/p/8478047.html