Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking file size before uploading

 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I have a form where i allow the user to upload files as much files as they want until they reach X mb.
Suppose that X = 1 mb, they can upload 1 file of 1 mb OR 2 files of 500 ko OR 5 files of 200 ko.

How did i try to do that :
In the event "onChange" of the "input file", i load a servlet which redirect the user to another jsp (popup).

CheckFile.java - GET


In the jsp, i made a form with an input file and i tryed to make a dynamic value for that input.. then using javascript, submit the form to send the file information trought the servlet "POST" method.


And then in the Servlet post method, i could check the size of the file, redirect to the jsp and, using js, tell the parent window that the user can still upload (1MB - filesize) Ko.

The problem is : You CAN'T change input file value...
I know that it's way much easyer and cleaner to use ajax, but i can't use it neither..

Can anyone know how i could do that without reloading the main jsp page?

Thanks. (and sorry if my english is not that good :$)
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use java script to check the file size using ActiveX objects.

check out the below link

http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript

Regards,
Jigar Naik
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand how this approach is supposed to work, but in any case, JavaScript has no access to file information - you can't know the file size unless you upload the file.

Any approach using ActiveX would be limited to a single browser on a single operating systems, and introduce all the security problems of ActiveX - not exactly what web apps are all about.
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jigar Naik wrote:You can use java script to check the file size using ActiveX objects.

check out the below link

http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript

Regards,
Jigar Naik


I can't use ActiveX Objects, they re blocked here, "security"..

Lester Burnham wrote:
you can't know the file size unless you upload the file.

That's why i tryed such a system.
I opened a popup and uploaded the file in this children window.. Then i could know the file size trought the children window servlet and tell it to the parent (main) window using "window.opener"
But i can't set the value of the input file popup window because this input is readonly.. That's the only thing blocking me..

Any other idea?

Thanks for your interest
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i can't set the value of the input file popup window because this input is readonly.

Correct, for security reasons the interaction between JavaScript and file inputs fields is limited.

I'm not sure what you're trying to accomplish, though. Do you want to display the combined size of all files that were uploaded so far? That would be better done in a DIV or SPAN, not the FILE field.
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to control the actual amount "uploaded".
So i can let the user select one more file to upload if the actual combined size is lower than the size limit.

e.g
The user is allowed to upload 1Mb. A label tell him 1 Mb max remaining.
1 input file is visible ... the user select a file of 200 kb -> 800 kb remaining
1 more input become visible .. the user select a file of 500 kb -> 300 kb remaining.
1 more input become visible ..

I hope you understand

Everytimes i want to make something "dynamic" i have to deal with javascript and make some complex stuff even if it's not something very hard to do :/
Some rules can't be broken ^^
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Leahcim wrote:Everytimes i want to make something "dynamic" i have to deal with javascript

Well yeah--how else would you make something dynamic other than by programming it?!
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Michael Leahcim wrote:Everytimes i want to make something "dynamic" i have to deal with javascript

Well yeah--how else would you make something dynamic other than by programming it?!


Well yeah.. I mean i'm stuck with old technologies... no ajax, no jquery,no activeX objects..
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why no Ajax? Ajax has been around for a decade.
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Why no Ajax? Ajax has been around for a decade.


Simply because I'm not working for myself, here, ActiveX.. are disabled from users Internet browser..
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AJAX does not involve ActiveX, just JavaScript (which in this day and age is unlikely to be disabled, since many sites depend on it).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE depends upon ActiveX to do Ajax. Talk about old technologies!

Someday Microsoft might "get it", but I'm not holding my breath.
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trust me, i can't use ajax anyway, i don't make rules unfortunately
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just simply create one file upload control. When user upload first file, on server side check if file size is more then one mb set some flag in requeat attribute along with the file name, size and display in the view that file can not be uploaded due to so and so reason. If the file size is less then 1 mb upload the file, on server set the file size, file name in request attribute, And display it on view so and so file has been uploaded on server of so and so mb. Follow the same algorythm for the second file upload and so on. This is the simplest way to upload a file on server without using ajax, don't need to open a popup window for file upload. If you want popup you need to play around with javascript.

Regards,
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just simply create one file upload control. When user upload first file, on server side check if file size is more then one mb set some flag in requeat attribute along with the file name, size and display in the view that file can not be uploaded due to so and so reason. If the file size is less then 1 mb upload the file, on server set the file size, file name in request attribute, And display it on view so and so file has been uploaded on server of so and so mb. Follow the same algorythm for the second file upload and so on. This is the simplest way to upload a file on server without using ajax, don't need to open a popup window for file upload. If you want popup you need to play around with javascript.

Regards,
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i think i'm going to do something like this.
Unfortunately, i'll have to reload the page each time the user select a file..

Thanks anyway for your interest guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!