zoukankan      html  css  js  c++  java
  • 【文件上传】

    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"/>

  • 相关阅读:
    用C#做成的程序如何打包部署,在其他PC机运行
    ZeroMQ——一个轻量级的消息通信组件 C#
    Google Protocol Buffers 快速入门(带生成C#源码的方法)
    【转】如何一直保持测试工作的热情
    【转】关于什么是测试专家的讨论
    释放对某端口的占用
    Android端手机测试体系
    【转】减少缺陷漏测的系统方法体系思考(10年经验的反思)
    【转】3种类型的测试专家之路选择
    【转】手机应用软件测试点汇总
  • 原文地址:https://www.cnblogs.com/baiboy/p/3145002.html
Copyright © 2011-2022 走看看