Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, October 5, 2013 8:25 AM
how to set size limit for ajaxfileupload?
All replies (3)
Saturday, October 5, 2013 6:39 PM âś…Answered
Did the web.config solutions not work?
Based on the documentation, you should be able to use one of the available events like UploadedComplete or UploadedStart similar to the example provided in my last response. You might try a suggestion similar to the one mentioned in this Stack Overflow discussion as seen below :
Client-Side Code
<script type="text/javascript">
function UploadComplete(sender, args) {
var filesize = args.get_fileSize();
var fileId = args.get_fileId();
var status = document.getElementById('AjaxFileUpload1_FileItemStatus_' + fileId);
var container = document.getElementById('AjaxFileUpload1_FileInfoContainer_' + fileId);
if (filesize > 72000) { // same condition used for server side
document.getElementById('lblStatus').innerText = "error";
if (status.innerText) {
status.innerText = " (Error)";
}
if (status.textContent) {
status.textContent = " (Error)";
}
container.style.color = 'Red';
}
}
</script>
Server-side Code
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
string savePath = MapPath("~/Images/" + e.FileName);
// dont save file & return if condition not matched.
if (e.FileSize > 72000) // use same condition in client side code
{
return;
}
AjaxFileUpload1.SaveAs(savePath);
}
catch (Exception ex)
{
throw ex;
}
}
Saturday, October 5, 2013 8:33 AM
There are a few ways to actually handle file size limits in ASP.NET. I'll review over a few of them below :
Checking the ContentLength property of your Uploaded File (server-side)
You'll just need to check the ContentLength property of your uploaded File which will return the number of bytes contained in the file. So if you wanted to determine if a file was less than 100kb, you would use the following statements :
//Check if it is null and it's ContentLength is less than 100kb (102400 bytes)
if(FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength < 102400)
{
//Your upload code here
}
else
{
//Don't upload - size limit exceeded (consider displaying an error)
}
and 100mb respectively :
//Check if it is null and it's ContentLength is less than 100mb (104857600 bytes)
if(FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength < 104857600)
{
//Your upload code here
}
else
{
//Don't upload - size limit exceeded (consider displaying an error)
}
Setting a Maximum Size within your web.config
You likely will need to update your maxRequestLength within your web.config to handle files and uploads that are large (as the default limit is often 4MB). This can be handled within the <system.web> section of your web.config or the <system.webServer> section if you want to handle it at the IIS level (both are probably a good idea).
It's important to know that maxAllowedContentLength is measured in bytes and maxRequestLength is measured in kilobytes when settings these values so you'll need to adjust them accordingly if you plan on handling much larger files :
<configuration>
<system.web>
<!-- This will handle requests up to 100KB (use 102400 for 100MB) -->
<httpRuntime maxRequestLength="100" timeout="3600" />
</system.web>
</configuration>
<!-- IIS Specific Targeting (noted by the system.webServer section) -->
<system.webServer>
<security>
<requestFiltering>
<!-- This will handle requests up to 100KB (use 104857600 for 100MB) -->
<requestLimits maxAllowedContentLength="102400" />
</requestFiltering>
</security>
</system.webServer>
Possible Client Side Solution for AJAX File Uploader
I came accross the following solution, which might be worth checking out as well :
<script type="text/javascript">
function AjaxFileUpload_UploadComplete(sender, e) {
//Grab your File size
var size = e.get_fileSize();
//It is too large, throw an error
if (size > 1048576) {
var err = new Error();
err.name = 'My API Input Error';
err.message = 'File Too Large! (Please use a smaller file)';
throw (err);
}
//Otherwise, it is fine!
else {
var div = document.getElementById("downSumary");
div.innerHTML += "<p>Uploaded: " + e.get_fileName() + "</p>";
}
}
</script>
Saturday, October 5, 2013 2:17 PM
thanks for your solution ,
Your responses are related to normal fileupload and I've already tried your last solution but unfortunately it not work