i want to restrict pdf file on file upload control i want java script for that i want to apply that java script in file upload event
+2
A:
You can check the file name on submit.
"hook to the <form>'s onsubmit with whatever method" {
filename = theFileElement.value;
if (!/\.pdf$/i.test(filename)) {
alert("error");
return false;
}
return true;
}
Note that this only checks if the file has an extension of .pdf. It does not (and cannot) check whether the file is really a just PDF or actually a nasty virus. Moreover, client side Javascript can easily be bypassed, so you should perform the check again on the server side.
KennyTM
2010-05-29 13:44:17
There are some APIs in modern releases of Firefox (and maybe the WebKit browsers) that allow some inspection of files. https://developer.mozilla.org/en/Using_files_from_web_applications
Pointy
2010-05-29 14:27:02
@Pointy: True, but you can't deny users from using IE.
KennyTM
2010-05-29 14:47:17
Of course; the point is that it's not absolutely impossible for the client to examine files being uploaded, and eventually even IE will have that ability. (We may be long dead by then, of course.)
Pointy
2010-05-29 14:59:02