C# 将文件上载到服务器时出现类型为“System.Web.HttpException”的异常
我是asp.net、ajax和c的新手。我试图通过以下示例在服务器上保存图像:C# 将文件上载到服务器时出现类型为“System.Web.HttpException”的异常,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我是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
<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>
及
您必须更改maxRequestLength和maxAllowedContentLength。。。附加信息:超过最大请求长度-看起来您已经有了所需的信息1尝试使用一个包含1个字符的小文件文本文件2如果有效搜索如何增加ASP.Net请求的最大请求长度这是否回答了您的问题?
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
</system.webServer>