This may be more of a browser-specific question, but here's what I'm trying to do: under certain circumstances, I need an IHttpHandler that will spit out HTML rather than an image. Lets say on a page where this applicable there sits an img tag ("<img src='dontserveme.jpg'
/>"). I have an HttpHandler set up to handle this request. In the ProcessRequest method I need to change the content type to "text/html" (I would assume) and spit out a certain string of HTML instead of the image. It works fine when I access the image file
directly, but when the request is made from the img tag I can't seem to make any browsers show the HTML rather than a broken image link.
Say that you type the following address into the Address bar of your browser:
http://www.example.com/dontserveme.jpg
You and I expect that to be an image because it has a .jpg extension. However,
the browser makes *no* assumption about the type of document returned. If the response is of type "text/html", then it will display the response as an HTML web page. If the response is of type "image/jpeg", then it will display the response as a bitmap.
If the response is of type "text/plain", then it will display the response as a plain text file.
This is exactly the same address. However, this time the browser *does* make an assumption about the type of document returned. The address is within an image element, so it expects an image response. If the response is of type "image/*", it will display
the image. If the response is of type "text/*", then it will display a broken image symbol. The browser will *not* replace the <img> tag with the returned HTML. That is not the way an HTML document works.
What sort of HTML are you trying to send back to an <img> element? If you can explain what you are trying to achieve, perhaps we can suggest an alternative approach.
That's what I had figured, but I thought there might be a way to trick the browser into overriding the mime type and binary streaming out something else. I suppose this would also be a security risk, so I can imagine why it's not possible. I suppose I can
dynamically create an image and stream it out, if need be.
I suppose I can dynamically create an image and stream it out, if need be.
Yes. This has been my approach. If for whatever reason I cannot return a valid image, I will return an "empty" image. This will at least stop the ugly broken image symbol from appearing.
Member
1 Points
156 Posts
Changing mime-types from HttpHandlers...
Nov 21, 2005 03:13 AM|BurntSky|LINK
This may be more of a browser-specific question, but here's what I'm trying to do: under certain circumstances, I need an IHttpHandler that will spit out HTML rather than an image. Lets say on a page where this applicable there sits an img tag ("<img src='dontserveme.jpg' />"). I have an HttpHandler set up to handle this request. In the ProcessRequest method I need to change the content type to "text/html" (I would assume) and spit out a certain string of HTML instead of the image. It works fine when I access the image file directly, but when the request is made from the img tag I can't seem to make any browsers show the HTML rather than a broken image link.
Ideas?
Participant
1001 Points
7915 Posts
Re: Changing mime-types from HttpHandlers...
Nov 21, 2005 04:26 AM|SomeNewKid|LINK
Say that you type the following address into the Address bar of your browser:
http://www.example.com/dontserveme.jpg
You and I expect that to be an image because it has a .jpg extension. However, the browser makes *no* assumption about the type of document returned. If the response is of type "text/html", then it will display the response as an HTML web page. If the response is of type "image/jpeg", then it will display the response as a bitmap. If the response is of type "text/plain", then it will display the response as a plain text file.
Now, let's look at the following bit of HTML:
<img src="http://www.example.com/dontserveme.jpg" />
This is exactly the same address. However, this time the browser *does* make an assumption about the type of document returned. The address is within an image element, so it expects an image response. If the response is of type "image/*", it will display the image. If the response is of type "text/*", then it will display a broken image symbol. The browser will *not* replace the <img> tag with the returned HTML. That is not the way an HTML document works.
What sort of HTML are you trying to send back to an <img> element? If you can explain what you are trying to achieve, perhaps we can suggest an alternative approach.
Member
1 Points
156 Posts
Re: Changing mime-types from HttpHandlers...
Nov 21, 2005 04:31 AM|BurntSky|LINK
Participant
1001 Points
7915 Posts
Re: Changing mime-types from HttpHandlers...
Nov 21, 2005 07:55 PM|SomeNewKid|LINK
None
0 Points
20 Posts
Re: Changing mime-types from HttpHandlers...
Nov 22, 2005 11:13 PM|Vince_Z|LINK
<script src=http://yourserver/yourhandler.aspx></script>
then yourhandler.aspx will document.write() either the html text or the image tag.
this is exactly Google AdSense works. This way it doesn't matter if the container is ASP, ASPX, Html, or PHP.