javascript - File input : Chrome is not checking if file still exist before submitting form -


i found bug made server crash during file upload.

when user clicks on file input, , chooses file upload. if user deletes or renames file before submitting form, chrome still sends empty file. (while firefox , ie handle error correctly)

after user has clicked on "submit" button, i've tryed check file size, not null.

        if (file.size == 0) {             notificationfactory.alertmessage({ messagetype: "error", message: "file missing" });         } 

i've handled error server-side, add validation client-side.

using javascript file api can pull size of file through object's size property using element.files[0].size. ensure file exists javascript, can simply:

if(typeof el.files[0] !== 'undefined' && el.files[0].size > 0) {     /* success! */ } 

we use typeof ensure file has been selected (el.files[0] undefined otherwise), , if passes check file size greater 0.

here's jsfiddle demo i've tested in chrome.

as important note, however, still need server-side validation users javascript disabled.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -