Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using jS to display an image from file

Reply
Thread Tools

Using jS to display an image from file

 
 
Tim Streater
Guest
Posts: n/a
 
      05-21-2008
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>
 
Reply With Quote
 
 
 
 
Álvaro G. Vicario
Guest
Posts: n/a
 
      05-21-2008
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
--
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-21-2008
On May 21, 8:28 pm, Tim Streater <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call an aspx that accepts parameters through HTTP POST and returns an image, and then display the image in my html? computer_guy ASP .Net 3 07-20-2007 04:25 AM
Display Image from Database in Image Control chrispragash@hotmail.com ASP .Net 1 10-18-2006 01:48 PM
<%=request.querystring("image")%> image won't display christina_nz3@yahoo.co.nz ASP General 1 07-14-2006 03:20 AM
Display Image from Database in an Image control chrispragash@hotmail.com ASP .Net Web Services 1 08-25-2005 07:38 PM
display image after the image downloaded =?Utf-8?B?UWluZG9uZyBaaGFuZw==?= ASP .Net 0 03-02-2004 11:46 PM



Advertisments