【文件上传】

html前台:

<body>
    <form id="form1" enctype="multipart/form-data" method="post" action="Handler1.ashx">

    <input type="file" name="myfile" value="123" /><br/>
    <input type="submit" name="file" />
    </form>
</body>

注意:enctype="multipart/form-data"设置后才能够提交服务器(一般处理程序) method="post"的设置

一般处理程序ashx:

public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //接收客户端上传文件
            HttpFileCollection fct = context.Request.Files;
            if (fct.Count > 0)
            {
                HttpPostedFile hpf = fct[0];
                //获取文件扩展名
                string ext = System.IO.Path.GetExtension(hpf.FileName);
                Random red = new Random();
                string date = DateTime.Now.ToString("yyMMddhhmmss");
                string filename = context.Request.MapPath("~/Image/" + date + red.Next(1000, 10000) + ext);

                //string Filename = System.IO.Path.GetFileName(hpf.FileName);
                //保存文件
                hpf.SaveAs(filename);
                context.Response.Write("上传文件成功");
            }
            else
            {
                context.Response.Write("请上传文件");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

这个不能防止空文件上传和格式控制:(图片格式可也用js代码判断,基于给户交互效果)

修改后:

     public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //接收客户端上传文件
            HttpFileCollection fct = context.Request.Files;
            if (fct.Count > 0)
            {
                HttpPostedFile hpf = fct[0];
                if (hpf.ContentLength > 0)//防止用户空文件上传
                {
                    if (hpf.ContentType == "image/jpeg" || hpf.ContentType == "image/png")
                    {
                        //获取文件扩展名
                        string ext = System.IO.Path.GetExtension(hpf.FileName);

                        Random red = new Random();
                        string date = DateTime.Now.ToString("yyMMddhhmmss");
                        string filename = context.Request.MapPath("~/Image/" + date + red.Next(1000, 10000) + ext);

                        //string Filename = System.IO.Path.GetFileName(hpf.FileName);
                        //保存文件
                        hpf.SaveAs(filename);
                        context.Response.Write("上传文件成功");
                    }
                    else
                    {
                        context.Response.Write("禁止上传!");
                    }
                }
                else
                {
                    context.Response.Write("上传失败。");
                }
            }
            else
            {
                context.Response.Write("请上传文件");
            }
        }
View Code

js控制:

<script type="text/javascript">
    function checkext() {
        var file = document.getElementById("file").value;
        var ext=file.substr(file.lastIndexOf(".")+1);
        if (ext.toLowerCase() == "jpg" || ext.toLowerCase() == "png") {
            return true;
         }
        else {
            alert("禁止上传");
            return false;
        }
    }
</script>

 控制文件上传大小:

在web.confige中<system.web>节点下配置:

 <httpRuntime maxRequestLength="1024000"/>

原文地址:https://www.cnblogs.com/baiboy/p/3145002.html