最大请求长度超出错误但文件大小未超过

时间:2016-10-22 20:08:24

标签: jquery asp.net ajax vb.net

我在同一页面上有两个文件输入元素。两者都允许多个文件上传。我使用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 Website \ mySite \ App_Code \ pgCde中的pgCde.Save文件() .vb:第684行

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

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

1 个答案:

答案 0 :(得分:0)

我终于得到了第二个文件输入,可以使用我在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>

我无法告诉你为什么会这样,但这是解决这个问题的解决方案。