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>
一般处理程序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 = ; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > )
{
//大小限制
int maxSize = * * ; //最大是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;
}
}
}
}
在上传一个超过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"/>
asp.net 一般处理程序接收上传文件的问题的更多相关文章
- asp dotnet core 支持客户端上传文件
本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...
- Spring Boot 在接收上传文件时,文件过大异常处理问题
Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...
- asp.net中FileUpload得到上传文件的完整路径
asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...
- ASP.NET MVC 4 批量上传文件
上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...
- ASP.NET MVC 4 Ajax上传文件
这两天一直纠结着表单的问题.想在一个表单里实现三个功能: 输入查询条件,点击查询: 导出查询数据: 上传文件: 方法有很多,乱花渐欲迷人眼,尝试了很多,无果.大致说的是,给不同按钮写js代码,在js代 ...
- Java Servlet 接收上传文件
在Java中使用 Servlet 来接收用户上传的文件,需要用到两个apache包,分别是 commons-fileupload 和 commons-io 包: 如果直接在doPost中,使用requ ...
- asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error
CSHTML代码 @{ if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { <input type=" ...
- ASP.NET Core MVC如何上传文件及处理大文件上传
用文件模型绑定接口:IFormFile (小文件上传) 当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发AS ...
- spring无法接收上传文件
现象 前端用ajax方式提交表单,代码类似于下面的例子. var formData = new FormData(); // HTML 文件类型input,由用户选择 formData.append( ...
随机推荐
- C/C++ -- Gui编程 -- Qt库的使用 -- 使用图片与动画
QWidget工程 #include "mywidget.h" #include "ui_mywidget.h" #include <QLabel> ...
- unity 图片变纯色填充
unity自带shader 即可
- Redis configuration
官方2.6配置如下: # Redis configuration file example # Note on units: when memory size is needed, it is pos ...
- win7下如何解决对方可以ping通我,但我ping不通对方问题
以下是在百度经验里面找到的文章:http://jingyan.baidu.com/article/6b97984da3ac851ca2b0bfe1.html 当我在虚拟机的linux系统中ping本机 ...
- SQL 集合例子
IF OBJECT_ID('tempdb..#Purchase', 'U') IS NOT NULL DROP TABLE #Purchase; CREATE TABLE #Purchase ( Pu ...
- SQL Server操作结果集-并集 差集 交集 结果集排序
操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或 ...
- 自己实现一个双向绑定的Vue
我们知道双向绑定是Vue的核心之一,接下来我们自己仿照Vue实现一个基本的功能. 项目代码在GitHub上: https://github.com/zhangKunUserGit/zk-vue
- Jsp&Servlet入门级项目全程实录第3讲
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.建立数据表及数据(略) 2.装载驱动,建立数据表 <link rel="stylesheet&qu ...
- Springboot --- Spring Security (一)
文章部分图片来自参考资料 问题 : Spring Security 内部实现的原理是什么 概述 Spring Security 是个安全框架,可以提供认证,防止网络功能等功能,可以结合 sprin ...
- ADO五大对象(转载)
来源:http://blog.csdn.net/u013201439/article/details/51111969 ADO五大对象(转载) 一.绪论 1.引言 在数据库应用系统中,必定要涉及到对数 ...