Can't Get Full Path From File Element

S

Smarty

Dear Friends,

I did a file type validation in Javascript. Just i want to check
whether a selected file is BMP or JPG. I wrote the following function
for it.

function checkType(){

Text = document.frm.file.value
if(Text.indexOf(":\\") == 1)
{

if("bmp" == Text.substring((Text.length - 3), Text.length) || "jpg"
== Text.substring((Text.length - 3), Text.length))
return true;
}
return false;

}

It is working Fine in IE6 ,Mozilla, Opera but it is not working IE7.
Because in IE7 if i put alert for document.frm.file.value it showing
only the file name, not file path (Ex: if i select a file e:\test.zip)
in IE7 it giving only test.zip. But i need the full path e:\test.zip
then only my first if condition will be satisfied. This problem only in
IE7.

Please help me is there any way to overcome this problem? or this is
problem of IE7.

Thanks in advance

Sriram.
 
R

RobG

Smarty said:
Dear Friends,

I did a file type validation in Javascript. Just i want to check
whether a selected file is BMP or JPG. I wrote the following function
for it.

function checkType(){

Text = document.frm.file.value

Use var to explicitly define the scope of variables, keep them local
unless a global scope is required.
if(Text.indexOf(":\\") == 1)

Why do you care about that?
{

if("bmp" == Text.substring((Text.length - 3), Text.length) || "jpg"
== Text.substring((Text.length - 3), Text.length))
return true;
}
return false;

}

Why not:

var fileName = document.frm.file.value;
var re = /.+\.(bmp|jpg)$/i;
return re.test(fileName);

Note that:

- filenames ending in .bmp or .jpg are not reliable indicators of the
actual format of the content;

- files might also fit the criteria for JPEG or bitmap even if they
have other extensions, such as .jpeg or .raw (or whatever else a user
might have specified) or no extension at all.

GIF and TIFF image formats are also popular, are you certain you want
to exclude them (just a question...)?
 
S

Smarty

Dear Rob,

Thanks for your reply. But i think you didnt get core of my doubt. Your
method and my method works in all browser except IE7 because

var fileName = document.frm.file.value;
alert(fileName)

if you selected a file e:\test.html then in IE7 alert msg shows only
test.html not e:\test.html

Thats is the main problem. Also one more thing in your example, if i
just type test.bmp with out selecting through browse button you method
wont work, bec u checking only the extension. We want to check the full
path

We just take this problem in the way how to make it work in IE7.

Thanks

Sriram
 
R

RobG

Smarty said:
Dear Rob,

Please don't top-post, reply below trimmed quotes.
Thanks for your reply. But i think you didnt get core of my doubt. Your
method and my method works in all browser except IE7 because

Your method fails in Safari and Firefox on Mac at least, where the
filename below will be returned as "/test.html" - note the lack of
protocol.
var fileName = document.frm.file.value;
alert(fileName)

if you selected a file e:\test.html then in IE7 alert msg shows only
test.html not e:\test.html

Then my method "works" in IE 7 too (sorry, I don't have it available
for testing) since it only cares about the last 5 characters of the
filename.
Thats is the main problem. Also one more thing in your example, if i
just type test.bmp with out selecting through browse button you method
wont work, bec u checking only the extension. We want to check the full
path

If you start from the premise that you can't reliably test the filename
using client-side script (assuming we are talking about a web
application) then you are on the right track. For your "test", I can
enter blah://blah.jpg and you'll think you've got a valid filename.

You can't test if the filename entered actually exists or if it is the
right type, all you can do is look at the extension and if it's not one
you like, suggest the user check it and, if necessary, try again. Let
them submit whatever they want and let the server sort it out.
We just take this problem in the way how to make it work in IE7.

No doubt there is a security setting somewhere to let you see the full
filename, but is that a reasonable solution? Will it really help?
 
S

Smarty

filename below will be returned as "/test.html" - note the lack of
protocol.




for testing) since it only cares about the last 5 characters of the
filename.



using client-side script (assuming we are talking about a web
application) then you are on the right track. For your "test", I can
enter blah://blah.jpg and you'll think you've got a valid filename.

You can't test if the filename entered actually exists or if it is the
right type, all you can do is look at the extension and if it's not one
you like, suggest the user check it and, if necessary, try again. Let
them submit whatever they want and let the server sort it out.



filename, but is that a reasonable solution? Will it really help?

Dear Rob,

Thanks for your reply. Yes you are right, user can cheat us any way,
only way is server side validation. I am going to check only the file
extension.

Take Care,

Sriram
 
R

Randy Webb

RobG said the following on 11/14/2006 4:04 AM:
Please don't top-post, reply below trimmed quotes.


Your method fails in Safari and Firefox on Mac at least, where the
filename below will be returned as "/test.html" - note the lack of
protocol.


Then my method "works" in IE 7 too (sorry, I don't have it available
for testing) since it only cares about the last 5 characters of the
filename.


It does "work" in IE7.
No doubt there is a security setting somewhere to let you see the full
filename, but is that a reasonable solution? Will it really help?

There is no setting in IE7 to expose the full path name as MS rightfully
decided you didn't need to know the full path. The only benefit the full
path will give anybody is to be able to potentially see the file
structure of the HD it is on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top