1

How can I effectively handle MaxUploadSizeExceededException in an ajax based file uploading operation.

I have an ajax based file upload functionality. I need to handle the MaxUploadSizeExceededException when user upload a file whose size exceeds the maximum allowed. I got the solution from here, where user will be directed to a error page, when file size exceeds. Since I am using ajax request to upload file, I want to show a javascript alert in the browser when file size exceeds, instead of redirecting to some error page.

How can I do this ?

Community
  • 1
  • 1
faizi
  • 1,265
  • 2
  • 12
  • 28

2 Answers2

1

A possible solution would be to completely follow the solution in your linked answer, where, on exception you would redirect to an error.jsp, which would in fact return JSON, so as simple as

error.jsp

<%@ page contentType="application/json;charset=UTF-8" language="java" %>
{"errors":"${errors}"}

in which case the success part of your ajax call would be

success : function(json) {
   alert(json.errors);
}
Community
  • 1
  • 1
Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

I don't know in Ajax, since you asked to show using javascript ,You can use ActiveXObject if you are using browsers that doesn't support HTML 5. If you are using modern browsers, you can use File APIs like below,

$('#fileId').bind('change', function() {
  //The below code will get the size of the uploaded file.
  alert(this.files[0].size);
});

You will get the file size in bytes, you can divide the file size by 1024 for getting in kb.

Kalaiarasan Manimaran
  • 1,598
  • 1
  • 12
  • 18