Reputation: 21
I have two file input elements on the same page. Both allow multiple file uploads. I am using jquery ajax and an asmx web service to upload image files. For some reason files are uploaded without any errors when the first file input elements is used, but gives a Maximum length exceeded error when the second input element is used. I am using test image files which do not exceed 4MB in size. This is the javascript code for the first input element.
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);
}
});
}
This is the code in the web service:
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
This code is used for both file input elements.
This is the javascript code for the second file input element:
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);
}
});
}
This is the error:
System.Web.HttpException: Maximum request length exceeded.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.GetMultipartContent()
at System.Web.HttpRequest.FillInFilesCollection()
at System.Web.HttpRequest.EnsureFiles()
at System.Web.HttpRequest.get_Files()
at pgCde.SaveFiles() in C:\Users\suzher\Documents\My Web Sites\mySite\App_Code\pgCde .vb:line 684
Line 684 points to this line of code in the web service:
Dim fCnt As Integer = Context.Request.Files.Count
Upvotes: 1
Views: 2059
Reputation: 21
I finally got the second file input to work with a combination of solutions I found here on Stack Overflow. The first solution was to go into the IIS Manager and stop and restart the Default Application Pool for ASP.Net (.NET CLR Version 4.0 in my case). This then enabled me to upload a few files at a time, but gave the Maximum request length error with more than 4 or 5 files. I then changed the web.config file and increased the maxRequestLength property to 24MB like this: (This has to be in the system.web section of the root web.config file.)
<system.web>
<httpRuntime targetFramework="4.5.2" maxRequestLength="24576" executionTimeout="3600"/>
</system.web>
I can't really tell you why this worked, but this is the solution that solved this problem.
Upvotes: 1