swapping images that are on local hard disk

Discussion in 'Javascript' started by TuftyTrue, Dec 18, 2006.

  1. TuftyTrue

    TuftyTrue Guest

    Hi,

    On my site the user can browse to an image with <input type=file etc>.
    Althrough I have no problem swapping images with:

    document.getElementById('PictureToDisplay').src =
    URLtoPictureOnWebServer.src;

    I cant get it to work with:

    document.getElementById('backgroundLayer').src =
    PathToPictureOnLocalDrive.src

    I've tried hardcodeing the variable (with and without quotes) with all
    the obvious values:
    "C:\pics\mypic.jpg", "file:///c:/pics/mypic.jpg" etc etc but cant get
    it to work.

    Any ideas why its fine with remote pictures, but not with local ones?

    (Also, just to muddy the waters even more, I've managed to display a
    local image using a style.setbackgroundimage="path to local image", but
    abandoned this as I cound not get the image to re-size :( )

    Any help gratefully recieved

    Regards

    Eadmund
     
    TuftyTrue, Dec 18, 2006
    #1
    1. Advertisements

  2. TuftyTrue

    Erwin Moller Guest

    Hi,

    AFAIK: What you want cannot be done with JavaScript.
    Local filesystem is not accesible via a random webpage's JavaScript (which
    is by design and good).
    The only exception to that rule is a file-upload, but that is crippled too
    so JavaScript cannot 'see' the values (files) used. (again by design).

    Some/many browsers let you retrieve html-files from the local filesystem
    (not via http), and then this restriction is lifted.
    I think this is called 'same origin policy' or something like that.
    So if the html-file originates from your local hd, the images can be found,
    if the html-file originates from some website (via http) your local
    filesystem is not accesible (not via a hardcoded link, and not via JS).

    This is good. Think about what would happen if any webpage could browse your
    filesystem behind your back...

    I think the only way out is:
    1) upload the file to the server
    2) offer the page with the (freshly uploaded) backgroundimage coming from
    the server (http).

    Maybe you can lift this restriction via 'trusted zones' in IE. Don't know.

    Just my 2 cent.

    Regards,
    Erwin Moller
     
    Erwin Moller, Dec 18, 2006
    #2
    1. Advertisements

  3. TuftyTrue

    TuftyTrue Guest

    Hi Erwin,

    Thanks for the quick reply. I think your right, I'm going to have to
    post the image the user has browsed to back to my site, then grab it
    back from the server.

    The irritating thing is that running the script while testing works
    fine when run from my desktop. I can even get image attributes such as
    hight and width and work out if its portrate and landscape and size my
    display accordingly. It all goes south when I run it from my web
    server, I'm assuming the browsers seurity contexts change somehow.

    The really odd thing is that I can change an image to one the user has
    browsed to by setting the backgroundLayer property, but as you cannot
    re-size this is javaascript, its no good in my environment :(

    Oh -well, back to perl side scripting again.....
     
    TuftyTrue, Dec 18, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.