【问题标题】:Maximum request length exceeded error but files sizes not exceeded超出最大请求长度错误,但未超出文件大小
【发布时间】:2017-03-04 22:33:02
【问题描述】:

我在同一页面上有两个文件输入元素。两者都允许多个文件上传。我正在使用 jquery ajax 和 asmx Web 服务来上传图像文件。由于某种原因,在使用第一个文件输入元素时文件上传没有任何错误,但在使用第二个输入元素时给出了最大长度超出错误。我正在使用大小不超过 4MB 的测试图像文件。这是第一个输入元素的 javascript 代码。

   function addCrs(){
    var nme = $("#crsName").val();
    var stp = "carousel";
    var upd = $("#crsAction option:selected").val();
    var fileUpload = $("#FileUpload1").get(0);
    var files = fileUpload.files;
    var test = new FormData();
    for (var i = 0; i < files.length; i++) {
        var fsize = files[i].size;
        var ftype = files[i].type;
        var fname = files[i].name;

        var fl = files.length;
        if (fl == 0 || fsize > 3774873.6 || ftype != 'image/jpeg') {
            $("label[for$=FileUpload1]").css("color", "Red");
        } else {
            $("label[for$=FileUpload1]").css("color", "#000000");
            test.append(files[i].name, files[i]);
        };
    }

    test.append("stype", stp);

    test.append("rname", nme);

    test.append("upd", upd);

        $.ajax({
            url: "pgCde.asmx/SaveFiles",
            type: "POST",
            enctype: 'multipart/form-data',
            contentType: false,
            processData: false,
            cache: false,
            dataType: "xml",
            data: test,
            success: function (result) {
                $xml = $(result),
      $str = $xml.find("string");
                var str = $str.text();


                if (str !== "This Carousel name already exists please type another name.") {
                    var n = str.split("_");

                    var crsl = Object();

                    crsl.crslName = $("#crsNameLst option:selected").val();
                    crsl.crslWidth = n[0];
                    crsl.crslHeight = n[1];
                    crsl.crslNumPhoto = n[2];
                    crsl.crslPage = $("#pgNameCrs option:selected").val();


                    var crsObj = { 'crsl': crsl };

                    $.ajax({
                        url: "pgCde.asmx/addCarousel",
                        type: "POST",
                        dataType: "json",
                        data: JSON.stringify(crsObj),
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            $("#crsConfirm").html(data.d);
                        },
                        error: function (e) {
                            $("#crsConfirm").html("Unavailable");
                        }


                    });
                } else {
                    $("#crsConfirm").html(str);
                }
            },
            error: function (err) {
                $("#crsConfirm").html(err.statusText);
            }
        });
}

这是网络服务中的代码:

Public Function uploadImage(ByVal fCnt As Integer, ByVal cnt As Integer, ByVal reNme As String) As String
    Dim rsp As String = ""
    If Context.Request.Files.Count > 0 Then
        Dim files As HttpFileCollection = Context.Request.Files

        Dim cntStr As String = ""

        cntStr = cnt.ToString
        For i As Integer = 0 To files.Count - 1

            cnt = cnt + 1

            If cnt <= 9 Then
                cntStr = "0" & cnt.ToString
            ElseIf cnt >= 10 Then
                cntStr = cnt.ToString
            End If
            Dim file As HttpPostedFile = files(i)
            Dim fname As String
            If HttpContext.Current.Request.Browser.Browser.ToUpper() = "IE" OrElse HttpContext.Current.Request.Browser.Browser.ToUpper() = "INTERNETEXPLORER" Then
                Dim testfiles As String() = file.FileName.Split(New Char() {"\"c})
                fname = testfiles(testfiles.Length - 1)
            Else
                fname = file.FileName
            End If


            Dim extF = file.ContentType
            Dim flLngth = file.ContentLength
            Dim fNme = fname.Split(".")
            Dim flExt = fNme(fNme.Length - 1)
            If flLngth <= 3774873.6 AndAlso extF = "image/jpeg" Then
                Dim nwPath = Path.Combine(Context.Server.MapPath("../Content/images/"), reNme & "_" & cntStr & "." & flExt)


                file.SaveAs(nwPath)
                Dim bmp As New Bitmap(nwPath)
                Dim wdth As String = bmp.Width.ToString()
                Dim hgt As String = bmp.Height.ToString()
                rsp = wdth & "_" & hgt & "_" & fCnt.ToString

                bmp.Dispose()

            Else
                rsp &= "Only jpeg files less than 3.6MB allowed."

            End If


        Next
    End If

    Return rsp
End Function
<WebMethod()>
Public Function SaveFiles() As String
    Dim rsp As String = ""
    Dim fCnt As Integer = Context.Request.Files.Count
    Dim reNme As String = Regex.Replace(Context.Request.Form("rname"), "\W", "_")
    Dim updt As String = Context.Request.Form("upd")
    Dim stp As String = Context.Request.Form("stype")
    Dim cnt As Integer = 0

    Dim thlm As New thalMem2DataContext
    If updt = "Add" Then
        If stp = "slideshow" Then

            Dim sEx = (From s In thlm.Slideshow_Infos Where s.sldName = reNme Select s).Count()
            If sEx = 0 Then
                rsp &= uploadImage(fCnt, cnt, reNme)
            Else
                rsp &= "This SlideShow name already exists please type another name."
            End If
        ElseIf stp = "carousel" Then

            Dim cEx = (From c In thlm.Carousel_Infos Where c.Carousel_Name = reNme Select c).Count()
            If cEx = 0 Then
                rsp &= uploadImage(fCnt, cnt, reNme)
            Else
                rsp &= "This Carousel name already exists please type another name."
            End If
        End If
    ElseIf updt = "Update" Then
        Dim numP As Integer

        If stp = "slideshow" Then
            numP = (From n In thlm.Slideshow_Infos Where n.sldName = reNme Select n.numSlides).Single

        ElseIf stp = "carousel" Then
            numP = (From n In thlm.Carousel_Infos Where n.Carousel_Name = reNme Select n.Num_Photos).Single
        End If

        Dim fAdd As Integer = fCnt + numP
            rsp &= uploadImage(fAdd, numP, reNme)
        End If

        Return rsp
End Function

此代码用于两个文件输入元素。

这是第二个文件输入元素的 javascript 代码:

function addSld() {
    var nme = $("#sldName").val();
    var stp = "slideshow";
    var upd = $("#sldAction option:selected").val();
    var fileUpload = $("#FileUpload2").get(0);
    var files = fileUpload.files;
    var test = new FormData();

    for (var i = 0; i < files.length; i++) {
        var fsize = files[i].size;
        var ftype = files[i].type;
        var fname = files[i].name;

        var fl = files.length;
        if (fl == 0 || fsize > 3774873.6 || ftype != 'image/jpeg') {
            $("label[for$=FileUpload2]").css("color", "Red");
        } else {
            $("label[for$=FileUpload2]").css("color", "#000000");
            test.append(files[i].name, files[i]);
        }
    }

    test.append("stype", stp);

    test.append("rname", nme);

    test.append("upd", upd);

 $.ajax({
            url: "pgCde.asmx/SaveFiles",
            type: "POST",
            enctype: 'multipart/form-data',
            contentType: false,
            processData: false,
            cache: false,
            dataType: "xml",
            data: test,
            success: function (result) {
                $xml = $(result),
      $str = $xml.find("string");
                var str = $str.text();


                if (str !== "This SlideShow name already exists please type another name.") {
                    var n = str.split("_");

                    var slide = Object();


                    slide.sldName = $.trim($("#sldName").val());
                    slide.sldOrient = $("#sldOrient option:selected").val();
                    slide.sldWidth = n[0];
                    slide.sldHeight = n[1];
                    slide.sldNumPhoto = n[2];
                    slide.sldPage = $("#pgNameSld option:selected").val();


                    var sldObj = { 'slide': slide };

                    $.ajax({
                        url: "pgCde.asmx/addSlideShw",
                        type: "POST",
                        dataType: "json",
                        data: JSON.stringify(sldObj),
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            $("#sldConfirm").html(data.d);
                        },
                        error: function (e) {
                            $("#sldConfirm").html("Unavailable");
                        }


                    });
                } else {
                    $("#sldConfirm").html(str);
                }
            },
            error: function (err) {
                $("#sldConfirm").html(err.statusText);
            }
        });
}

这是错误:

System.Web.HttpException:超出最大请求长度。

在 System.Web.HttpRequest.GetEntireRawContent()

在 System.Web.HttpRequest.GetMultipartContent()

在 System.Web.HttpRequest.FillInFilesCollection()

在 System.Web.HttpRequest.EnsureFiles()

在 System.Web.HttpRequest.get_Files()

在 C:\Users\suzher\Documents\My Web Sites\mySite\App_Code\pgCde 中的 pgCde.SaveFiles() .vb:第 684 行

第 684 行指向 web 服务中的这行代码:

 Dim fCnt As Integer = Context.Request.Files.Count

【问题讨论】:

    标签: jquery asp.net ajax vb.net


    【解决方案1】:

    我终于得到了第二个文件输入,可以使用我在 Stack Overflow 上找到的解决方案组合。第一个解决方案是进入 IIS 管理器并停止并重新启动 ASP.Net 的默认应用程序池(在我的情况下为 .NET CLR 版本 4.0)。这使我能够一次上传几个文件,但在超过 4 或 5 个文件时给出了最大请求长度错误。然后我更改了 web.config 文件并将 maxRequestLength 属性增加到 24MB,如下所示: (这必须在根 web.config 文件的 system.web 部分中。)

    <system.web>
    <httpRuntime targetFramework="4.5.2" maxRequestLength="24576" executionTimeout="3600"/>
    </system.web>
    

    我无法真正告诉您为什么这样做,但这是解决此问题的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多