By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Can't Get Full Path From File Element

P: n/a
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.

Nov 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Smarty wrote:
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...)?
--
Rob

Nov 14 '06 #2

P: n/a
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
On Nov 14, 12:36 pm, "RobG" <r...@iinet.net.auwrote:
Smarty wrote:
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.valueUse 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...)?

--
Rob
Nov 14 '06 #3

P: n/a
Smarty wrote:
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?
--
Rob

Nov 14 '06 #4

P: n/a


On Nov 14, 2:04 pm, "RobG" <r...@iinet.net.auwrote:
Smarty wrote:
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 becauseYour 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.htmlThen 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
pathIf 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?

--
Rob
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

Nov 14 '06 #5

P: n/a
RobG said the following on 11/14/2006 4:04 AM:
Smarty wrote:
>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.

It does "work" in IE7.
>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?
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.