First of all, my answer will only work if you use ASP.NET (C #), but it can be easily ported to VB.NET, PHP or something like that.
If you load an image in javascript, it will look like a dead url and will cause an error when trying to load it.
What I did was create a page that gets the URL of the image in the query line, reads the image and displays the response.
Webform.aspx
<script type="text/javascript"> $(document).ready(function () { var image_url = "http://www.kottke.org/plus/misc/images/castro-pitching.jpg"; var $image = $('<img src=LoadImage.aspx?imageUrl="' + image_url + '" width="50" height="50" />'); $('body').prepend($image); }); </script>
LoadImage.aspx (.cs code-Behind)
using System; using System.Net; public partial class LoadImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String url = Server.UrlDecode(Request.QueryString["imageUrl"]).Replace("\"", ""); WebRequest req = WebRequest.Create(url); System.Drawing.Image img = System.Drawing.Image.FromStream(req.GetResponse().GetResponseStream()); Response.Clear(); Response.ContentType = "image/jpeg"; img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } }
It is like successfully loading an image and displaying it, redirecting 302 and thatโs all ... There is no way that I know, anyway, to do this without some work on the server side.
If you cannot use .NET but can use a different language, or if you cannot use any other language at all, let me know ...
source share