Skip to content Skip to sidebar Skip to footer

How Stop File Upload Event Using Javascript

I want to stop file upload event when i upload the file more than 5mb or if my file extension is not .jpg or .png. I found a code but when i try to upload the invalid file type the

Solution 1:

You need to clear the value of 'sec_2_img'. You do this by adding fieldObj.value = ""; after alert(error) and before return false;. This will remove the file name from sec_2_img and force user to select a new one while still showing the message so user knows what to do.

Solution 2:

1.) Don't use alerts, they are annoying and the browsers will block them pretty fast, so your users won't get any message anymore. Use a modal window or add a styled element with the error-message somewhere into your form.

2.) If you need to have a valid file to submit this form, you have to add a event-handler to the submit-event of the form and stop the trasmission.

3.) your check for file-extension is case-sensitive, this might give you problems on windows, and your calculation of the fileSize is wrong.

Post a Comment for "How Stop File Upload Event Using Javascript"