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

Using jS to display an image from file

P: n/a
The following test page is intended to allow the user to choose an image
file, and then display it. It works as expected in Safari 3.1.1, FF
2.0.0.14 (Mac), and IE7 (XP).

But, it fails in FF 2.0.0.14 (Win-XP) - the image doesn't appear. The
error console shows no errors and Page Info, under the media tab, shows
no loaded image (in contrast to FF Mac which shows the image).

The images I select are all jpegs, btw.

Any pointers would be appreciated - thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">

<html>
<head>
<title>Image with a Javascript preview.</title>
<script type="text/javascript">
function preview()
{
var filename = document.form1.filesent.value;
var Img = new Image ();
Img.src = filename;
document.images[0].src = Img.src;
alert ("Filename: '" + filename + "' width: " + Img.width + "
height: " + Img.height);
}

</script>
</head>

<body>
<h3>File Upload - v020</h3>
<form name='form1'>
<input type=file name=filesent>
<input type=button value="Preview" name="Preview" onClick="preview()">
</form>
<p>Image follows</p>
<img name="image1">
</body>
</html>
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tim Streater escribió:
var filename = document.form1.filesent.value;
var Img = new Image ();
Img.src = filename;
A string like "C:\Picture.jpg" is not a valid URI. Try adding a
"file://" prefix and maybe escaping file name characters that need so.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 27 '08 #2

P: n/a
VK
On May 21, 8:28 pm, Tim Streater <tim.strea...@dante.org.ukwrote:
The following test page is intended to allow the user to choose an image
file, and then display it. It works as expected in Safari 3.1.1, FF
2.0.0.14 (Mac), and IE7 (XP).

But, it fails in FF 2.0.0.14 (Win-XP) - the image doesn't appear. The
error console shows no errors and Page Info, under the media tab, shows
no loaded image (in contrast to FF Mac which shows the image).

The images I select are all jpegs, btw.

Any pointers would be appreciated - thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">

<html>
<head>
<title>Image with a Javascript preview.</title>
<script type="text/javascript">

function preview()
{
var filename = document.form1.filesent.value;
var Img = new Image ();
Img.src = filename;
document.images[0].src = Img.src;
alert ("Filename: '" + filename + "' width: " + Img.width + "
height: " + Img.height);
}

</script>
</head>

<body>
<h3>File Upload - v020</h3>
<form name='form1'>
<input type=file name=filesent>
<input type=button value="Preview" name="Preview" onClick="preview()">
</form>
<p>Image follows</p>
<img name="image1">
</body>
</html>
Within the standard security settings you are not allowed to read the
current input-file content, only the file name itself, without path.

IE and some other browsers are having a bit relaxed policy so they let
you to read the full path iff the current page is loaded from a local
drive. FF doesn't make such relaxation in neither case: this is why
the difference you see.

For a page loaded from the Web all browsers are acting in the same way
(file name only, no path) and sometimes even that is prohibited.

In any case no one browser will not let you to access the local file
system in the latter case, so even if you set the path manuially like
myImg.src = "file:///C:/image.gif" if will not work.

So the starting idea was nice but it didn't account the necessarily
imposed security restrictions.

For the preview if you still want to implement it, you have to submit
the image first to the server and having server bouncing the image to
the form's target (i)frame.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.