zoukankan      html  css  js  c++  java
  • asp.net 一般处理程序接收上传文件的问题

    在使用Html+ashx处理文件上传时,遇到上传文件超过4M的问题,首先HTML代码如下:

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <div>
            单文件上传
            <form enctype="multipart/form-data" method="post" action="UpLoadHandler.ashx">
                <input type="file" name="files" />
                <input type="submit" value="上传" />
            </form>
        </div>
    
        <div>
            多文件上传
            <form enctype="multipart/form-data" method="post" action="UpLoadHandler.ashx">
                <input type="file" name="files" multiple />
                <input type="submit" value="上传" />
            </form>
        </div>
    </body>
    </html>
    View Code

    一般处理程序UpLoadHandler.ashx的代码如下:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace Zhong.Web
    {
        /// <summary>
        /// UpLoadHandler 的摘要说明
        /// </summary>
        public class UpLoadHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //获取所有的上传文件
                //如果上传的文件超过了4M,则会报异常[超过了最大请求长度。],需要在Web.config配置
                HttpFileCollection hfc = context.Request.Files;
                //如果是hfc.Count为0,则可以认为是直接请求该ashx处理程序
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        //大小限制
                        int maxSize = 2 * 1024 * 1024;  //最大是2M(转换为字节)
                        if (hpf.ContentLength > maxSize)
                        {
                            context.Response.Write("上传文件大小超出限制");
                            return;
                        }
                        //扩展名限制
                        string[] exts = { "image/jpg", "image/jpeg", "image/gif", "image/png" };
                        if (!exts.Contains(hpf.ContentType.ToLower()))
                        {
                            context.Response.Write("必须上传图片文件");
                            return; 
                        }
                        string fileName = Path.GetFileName(hpf.FileName);
                        hpf.SaveAs(context.Server.MapPath("~/UpLoad/" + fileName));
                    }
                }
    
                ////获取指定name的文件上传
                ////该方式如果是html5的<input type="file" name="files" multiple />则始终是获取第一个上传的文件,multiple
                ////是支持多个文件上传,所以如果上传文件多于一个,则会丢失
                //HttpPostedFile hpf = context.Request.Files["files"];
                //if (hpf!= null && hpf.ContentLength > 0)
                //{
                //    hpf.SaveAs(context.Server.MapPath("~/UpLoad/" + Path.GetFileName(hpf.FileName)));
                //}
                context.Response.Write("上传成功");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    View Code

    在上传一个超过4M的文件时,异常如下:

    这时可以通过配置web.config修改文件上传的大小限制。

    <?xml version="1.0" encoding="utf-8"?>
    
    <!--
      有关如何配置 ASP.NET 应用程序的详细信息,请访问
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    
    <configuration>
        <system.web>
          <!--maxRequestLength的单位是kb,最大不能超过2097151 Kb即4GB,executionTimeout执行超时,单位是秒-->
          <httpRuntime maxRequestLength="2097151" executionTimeout="600"/>
          <compilation debug="true" targetFramework="4.0" />
        </system.web>
    
    </configuration>

    此处增加了一行<httpRuntime maxRequestLength="2097151" executionTimeout="600"/>

  • 相关阅读:
    复杂报表的存储过程
    Jquery中使用setInterval和setTimeout
    Jquery EasyUi实战教程布局篇
    枚举enum
    myGeneration代码生成器
    带有分页的存储过程
    应用临时表的存储过程
    缓存类的写法
    HDU4706 Children's Day
    HDU4706 Children's Day
  • 原文地址:https://www.cnblogs.com/godbell/p/7149841.html
Copyright © 2011-2022 走看看