c# - 将文件上传到服务器时出现“System.Web.HttpException”类型的异常
问题描述
我是 asp.net、ajax 和 c# 的新手。我正在尝试通过以下示例将图像保存在服务器上:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="file" name="postedFile" />
<input type="button" id="btnUpload" value="Upload" />
<progress id="fileProgress" style="display: none"></progress>
<hr />
<span id="lblMessage" style="color: Green"></span>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("click", "#btnUpload", function () {
$.ajax({
url: 'Handler.ashx',
type: 'POST',
data: new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function (file) {
$("#fileProgress").hide();
$("#lblMessage").html("<b>" + file.name + "</b> has been uploaded.");
},
xhr: function () {
var fileXhr = $.ajaxSettings.xhr();
if (fileXhr.upload) {
$("progress").show();
fileXhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
$("#fileProgress").attr({
value: e.loaded,
max: e.total
});
}
}, false);
}
return fileXhr;
}
});
});
</script>
</form>
</body>
</html>
添加了通用处理程序:
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
namespace RateEatPresentation
{
/// <summary>
/// Summary description for Handler
/// </summary>
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//Check if Request is to Upload the File.
if (context.Request.Files.Count > 0)
{
//Fetch the Uploaded File.
HttpPostedFile postedFile = context.Request.Files[0];
//Set the Folder Path.
string folderPath = context.Server.MapPath("~/UsersUploadedImages/");
//Set the File Name.
string fileName = Path.GetFileName(postedFile.FileName);
//Save the File in Folder.
postedFile.SaveAs(folderPath + fileName);
//Send File details in a JSON Response.
string json = new JavaScriptSerializer().Serialize(
new
{
name = fileName
});
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "text/json";
context.Response.Write(json);
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
但是我得到了异常“System.Web.dll 中发生了'System.Web.HttpException'类型的异常,但未在用户代码中处理”。我试图找到类似的东西,但不幸的是没有成功:
解决方案
图片可能太大了,你需要调整 web.config 来支持它们。默认情况下 IIS 支持 4MB,您可以在 Web.config 中更改它
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
和
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
</system.webServer>
您必须更改 maxRequestLength 和 maxAllowedContentLength
推荐阅读
- java - 找不到 PropertySource:Spring Cloud 配置 URL 的 PKIX 路径构建失败
- powershell - 如何从特定模式中获取子字符串
- java - 删除 IntelliJ 中的方法行
- ffmpeg - 无法使用 FFMPEG CLI 将缩略图添加到 MP4
- ui-automation - 在 Ghost 检查器中设置变量以单击网站上的卡片/图块
- cassandra - 在 cassandra 中更新表现为 upsert
- javascript - 仅禁用滚动到 onclick href 元素
- python - 用用户定义的函数聚合 df
- javascript - EventListener 'drop' - 获取丢弃的元素 id?-香草JS
- javascript - 下载文件时,我收到来自 Web API 调用的响应