簡體   English   中英

將文件上傳到服務器時出現“System.Web.HttpException”類型的異常

[英]exception of type 'System.Web.HttpException' while uploading file to server

我是 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM