Results 1 to 9 of 9

Thread: Issue with loading an image {jpg}

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Issue with loading an image {jpg}

    I have a web page that needs to dynamically load an image into an img tag. This works fine if the image is stored in the same folder as the aspx or if I store the images in a sub folder. The issue is that the customer wants to store the images elsewhere on a different drive, different machine and of course if I use the physical path in the img tag nothing gets displayed.

    Is there a way I can get a jpg from a physical path to be displayed within an img tag?

    I have some code that can send the content of a file as a stream and display it in the browser when that file is referenced by the physical path because the browser doesn't get the path just the content but this method does not seem to work if it needs to be within an img tag.

    Surely there is a way to do this but so far I have not came across anything on the net.

    Any suggestions?

    I guess it shouldn't matter but the page in question is HTML and uses JS to call an ASPX written in C# which returns the path\filename of the image to use then the calling HTML page processes the return via JS to change the content of the <img> on the fly. Works fine when it is using relative path but becomes an issue when a physical path is used.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Issue with loading an image {jpg}

    Image tags work best with relative paths. If you want an absolute path, it has to be a URL. I don't believe there is anyway to make something like "C:\Images\MyImage.png" to work.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Issue with loading an image {jpg}

    Does a url have to link though a web server.
    The application in question is on a lan but the images are on a different server than the web site.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Issue with loading an image {jpg}

    Try using a URI. That's might work. A URL is really just a subset, a type of URI.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Issue with loading an image {jpg}

    Quote Originally Posted by DataMiser View Post
    I have some code that can send the content of a file as a stream ...
    ...but this method does not seem to work if it needs to be within an img tag.
    If the (binary Stream) http-Response (from your serverside Script) -
    is not "flagged" as one of the supported types the (clientside) img-tag supports,
    then the Browsers "img-src-Loader" ignores that ("anonymous") response-content.

    In other words... do it right - and set the proper http Content-Type (Mime) header on your Response-Object,
    (from within your serverside script, to give the receiving Browser a hint, what to do with "those Stream-Bytes").

    Same thing holds true, when you e.g. retrieve "PDF-Bytes from a DB-Blob-Field" dynamically at the serverside...
    (which need to be flagged as Mime-Type "application/pdf" when you send them back to the Browser)...

    Olaf
    Last edited by Schmidt; Feb 11th, 2023 at 04:56 AM.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Issue with loading an image {jpg}

    What web server is the aspx site hosted on (IIS / apache)? Would the client browser have access, via http(s), to the different machine that is hosting the images?

    Edit: Had an idea...

    Have you considered an ashx file? https://learn.microsoft.com/en-us/tr...dules-handlers

    You would effectively implement the IHttpHandler interface and return a byte stream containing the jpeg contents, something like the following would be a very minimal outline.

    Code:
    public class JpegHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //Get image path / id from querystring / somewhere
    
            //Read image into byte array
    
            //return file
            context.Response.ContentType = "application/octet-stream";
            context.Response.BinaryWrite(bytes);
        }
    }
    The html page could use
    Code:
    <img src="path to handler, not to physical file">
    to display the image.

    This would require the web server to have access to the servers hosting the images.
    Last edited by PlausiblyDamp; Feb 11th, 2023 at 05:50 AM.

  7. #7
    New Member
    Join Date
    Feb 2023
    Posts
    9

    Re: Issue with loading an image {jpg}

    This is almost a common problem.

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Issue with loading an image {jpg}

    Quote Originally Posted by PlausiblyDamp View Post
    Code:
           ....
            //return file
            context.Response.ContentType = "application/octet-stream";
            context.Response.BinaryWrite(bytes);
        }
    }
    No, that'd still confuse the Browser, when it tries to interpret the response for an <img> resource...
    The dynamic "Script-response" (when it sends Bytes), should make very clear "what these Bytes are":
    A stream send in response to an "image-tag" needs to have an image-MimeType:
    https://developer.mozilla.org/en-US/...es#image_types

    One can deduce, which kind of ContentType to set (at the serverside) -
    e.g. from the "FileEnding of the passed relative FileParam".

    Olaf

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Issue with loading an image {jpg}

    Quote Originally Posted by Schmidt View Post
    No, that'd still confuse the Browser, when it tries to interpret the response for an <img> resource...
    The dynamic "Script-response" (when it sends Bytes), should make very clear "what these Bytes are":
    A stream send in response to an "image-tag" needs to have an image-MimeType:
    https://developer.mozilla.org/en-US/...es#image_types

    One can deduce, which kind of ContentType to set (at the serverside) -
    e.g. from the "FileEnding of the passed relative FileParam".

    Olaf
    Good catch, that was a hasty copy and paste from an old project and I forgot to change the content type! If it was a jpeg image then image/jpeg would have made a lot more sense!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width