-
Feb 10th, 2023, 08:36 PM
#1
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.
-
Feb 10th, 2023, 08:54 PM
#2
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.
-
Feb 10th, 2023, 09:00 PM
#3
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.
-
Feb 10th, 2023, 09:27 PM
#4
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.
-
Feb 11th, 2023, 04:52 AM
#5
Re: Issue with loading an image {jpg}
Originally Posted by DataMiser
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.
-
Feb 11th, 2023, 05:42 AM
#6
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.
-
Feb 11th, 2023, 06:25 AM
#7
New Member
Re: Issue with loading an image {jpg}
This is almost a common problem.
-
Feb 11th, 2023, 12:18 PM
#8
Re: Issue with loading an image {jpg}
Originally Posted by PlausiblyDamp
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
-
Feb 11th, 2023, 12:29 PM
#9
Re: Issue with loading an image {jpg}
Originally Posted by Schmidt
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|