locked
Handler to return image/links RRS feed

  • Question

  • User74713247 posted

    Please could someone help?

     I'm writing a handler which needs to send back images and links to appear on a clients external site from a single line of javascript on the client site e.g.

    <script src="http://www.mysite.com.handler.ashx?ref=1234" type="text/javascript"></script>

     
    Dim oBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
    oBuilder.Append("<table><tr><td>Test</td></tr></table>")
    context.Response.ContentType = "text/javascript"
    context.Response.Write("document.write('" + JavaReplace(oBuilder.ToString) + "');") 'JavaReplace simply swaps out characters.

     
    This all works great but I need to insert images into the HTML. I can reference the images with:

    Dim imageBytes As Byte()
    Dim im As Image = Image.FromFile(FILENAME)
    Dim stream As MemoryStream = New MemoryStream()
    im.Save(stream, ImageFormat.Gif)
    stream.Close()
    im.Dispose()
    imageBytes = stream.GetBuffer()

    But how do I send them to the client - I'm guessing I have to stream them, something like this:

    'Send back image.
    context.Response.ContentType = "image/gif"
    context.Response.BufferOutput = False
    context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length)
     

    but I'm having no joy whatsoever, please could someone help, thanks 

    Friday, December 15, 2006 10:58 PM

Answers

  • User1109032460 posted

    What are you trying to achieve? Are you writing a control? Why do you need to return an image reference in javascript and manipulate the DOM just to display an image?

    If the images have known addresses, then just put <img src="url of image" /> on the page. If you want to do something programmatically, then write a handler that returns images and then put <img src="url of handler" /> on the page. If you want to make it appear that the images are generated on the client site, not yours, then write a handler that the client installs that requests the image data from your site and then returns it. No javascript or DOM manipulation is required whatsoever to achieve that.

    If you want to simplify their page design, write a server-side control that emits the HTML containing the call to the handler (e.g. wraps <img src="url of handler" />), but that seems a little pointless unless there is more HTML to be generated (e.g. you want the <img> tag to appear inside a table, or come with client-side script for some other purpose).

    Remember that for each request from the browser you can return one type of data, whether that's HTML, image data or javascript. You cannot write a single handler to return all three, without writing some awful switch code based on, say, a query string parameter: and that simply wouldn't be a sane idea.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 17, 2006 9:59 PM

All replies

  • User1109032460 posted

    Just do

    Bitmap bmp = (Bitmap) Image.FromFile( FILENAME );
    bmp.Save( context.Response.OutputStream, ImageFormat.xxx )
    context.Response.ContentType = "image/xxx"
    bmp.Dispose()

    where xxx is gif, jpg, etc.

    Bearing in mind that saving an image as a GIF in GDI+ yields (typically) very poor results, because of its choice of a standard palette. You might want to check out MSDN, which has an article on this. I can't remember the URL at the moment, and I have to dash, but a quick Google search for GIF, GDI+, MSDN and octree should get you there.

    Saturday, December 16, 2006 2:31 AM
  • User74713247 posted

    Thanks DMW I'll attempt to have another go tonight. I'll let you know how I get on...

     I'm looking to use this technique to serve ads from my website.
     

    Saturday, December 16, 2006 2:04 PM
  • User74713247 posted

    I can successfully write images with context.Response.OutputStream.Write(imageBytes,0,imageBytes.Length) if I reference the handler directly, however I need a single line of javascript

    <script src="http://www.mysite.com/handler.ashx?ref=1234" type="text/javascript"></script>

     to talk to this handler (sits on client site) and display the images etc. The script is expecting javascript back so errors with something like

    illegal character
    handler.ashx?in... (line 1)
    GIF89a2
     
    Should I be streaming? How do I do this? Thanks KowDot 

     

    Sunday, December 17, 2006 1:22 PM
  • User1109032460 posted

    If yoiu've got a <script> element with its type set to "text/javascript"> then unsurprisingly the browser going to be might unhappy when you send it a byte stream from a GIF.

    You should be using <img src="..." /> to display the image.

    Sunday, December 17, 2006 7:30 PM
  • User74713247 posted

    The images are stored on my site and not the clients! How do I reference the images on my site to appear on the clients!

    Do I just use for example: <img src="http://mysite.com/myimages/image1.gif" ... etc. ?

    I'm guessing as well that I'll have to send the image reference in javascript too by adding to the DOM or add a div and use .innerhtml to send back pure html with the image reference to my site?

    Thanks

    Sunday, December 17, 2006 8:31 PM
  • User1109032460 posted

    What are you trying to achieve? Are you writing a control? Why do you need to return an image reference in javascript and manipulate the DOM just to display an image?

    If the images have known addresses, then just put <img src="url of image" /> on the page. If you want to do something programmatically, then write a handler that returns images and then put <img src="url of handler" /> on the page. If you want to make it appear that the images are generated on the client site, not yours, then write a handler that the client installs that requests the image data from your site and then returns it. No javascript or DOM manipulation is required whatsoever to achieve that.

    If you want to simplify their page design, write a server-side control that emits the HTML containing the call to the handler (e.g. wraps <img src="url of handler" />), but that seems a little pointless unless there is more HTML to be generated (e.g. you want the <img> tag to appear inside a table, or come with client-side script for some other purpose).

    Remember that for each request from the browser you can return one type of data, whether that's HTML, image data or javascript. You cannot write a single handler to return all three, without writing some awful switch code based on, say, a query string parameter: and that simply wouldn't be a sane idea.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 17, 2006 9:59 PM
  • User74713247 posted
    Thanks DMW, you're explanations were excellent. My understanding (or lack of!) was not very good. I'm basically allowing any site (potentially 100's!) to paste a single line of script into their sites. This script talks to my site which throws back ads. These ads are image links within some surrounding html e.g. a table etc. From your explanation I believe I now have the solution - we'll see tonight when I code it!!! Here is what I am going to do: Create a generic handler to pass back javascript (and cache it). Within that javascript a new div will be added by manipulating the dom (have the working code to do this). I can then use div.innerhtml to insert all the html that I need. The src of each image will point to another generic handler which sends back images. The reason for this second handler is because I want to cache the images both to save time but also because the filenames can change. Thanks for pointing out that each request can only return one type of data since I was trying to return different types!!! Thanks ever so much for your help. I'll let you know how I get on.
    Monday, December 18, 2006 8:00 AM
  • User74713247 posted

    Thanks Dave, Managed to get it all working quite easily last night. It's amazing what can be done when you have the correct knowledge. Thanks again for all your help, you have saved me hours and hours :)

     

    Tuesday, December 19, 2006 7:45 AM