how to know which Image has been requested C#,ASP.Net

后端 未结 4 1761
遥遥无期
遥遥无期 2021-01-28 12:09

I am developing a web app. which will generate a random link pointing to an image on my server. something like -http://dummy.com/Images/Image1.jpg?id=19234

Here this lin

相关标签:
4条回答
  • 2021-01-28 12:38

    You can change your image extension to an aspx extension (http://dummy.com/Images/Image1.aspx?id=19234), there is no problem in this, because this page the only thing it would do Response.OutputStream of the image. That is to say it would be similar to a jpg but with the advantage you can have some other code to process.

    In this aspx (before outputing the image), we would ask about the http_referer and it would be stored in a data table if this registry does not exist.

    This is really useful if for example you want to restrict the access to images. You could add some logic to forbid if they are not logged in.

    0 讨论(0)
  • 2021-01-28 12:48

    Oddthinking is right. See http://code.google.com/intl/en/apis/ajaxsearch/documentation/#fonje_snippets or Google's API. They give examples for PHP and Java, but there are also AJAX frameworks for ASP.NET (http://www.asp.net/ajax/), and I'm sure C# as well.

    0 讨论(0)
  • 2021-01-28 12:49

    Is this as simple as Googling? Search for

    link:http://dummy.com/Images/Image1.jpg?id=19234
    

    If you want to do this programmatically, you'll need to use the Google API.

    0 讨论(0)
  • 2021-01-28 12:50

    The issue you'd have with an HttpHandler is that it will generally only kick in for requests that are being handled by the ASP.Net engine - the image requests will normally be handled by IIS without going through the handler.

    Your web logs should be able to tell you who the referers for any given item on your servers are - assuming that you have them, and you hve something to process them - this will be more accurate than using Google.

    Going forward, one of the ways I've done this in the past is to have the image generated by an HttpHandler (implementing IHttpHandler).

    This will return the image as a stream (setting the content type to "image/jpeg"), and you can add further processing (such as logging where the request (referer) came from, etc).

    The limitation I found with the HttpHandler, is that some services (PBBS for example) require an image link to have an image extension - I got around this by processing all 404's with an ASP.Net page that checks for the .jpg extension in the request. If it finds one, instead of returning the usual 404 page, it returns the requeted image. You'll need to configure the 404 handler in IIS though, as the web.config error handler only kicks in for ASP.Net requests (web services and .aspx type pages).

    Example handler:

    // Sample from the ASP.Net Personal Web Site Starter Kit
    public class Handler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            // Set up the response settings
            context.Response.ContentType = "image/jpeg";
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.BufferOutput = false;
    
            // QueryString parameters are available here:
            // context.Request.QueryString["QueryStringKey"]
    
            // You can also access the Referrer object, and log the requests here.
    
            Stream stream;
            // Read your image into the stream, either from file system or DB
            if (stream == null)
            {
                stream = PhotoManager.GetPhoto();
            }
    
            // Write image stream to the response stream
            const int buffersize = 1024 * 16;
            var buffer = new byte[buffersize];
            int count = stream.Read(buffer, 0, buffersize);
            while (count > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, count);
                count = stream.Read(buffer, 0, buffersize);
            }
        }
    }
    

    You can have similar code (or better yet, refactor the main image streaming code into a shared class) in the 404 page, that checks for the existence of the image extension, and renders the image out that way (again, setting the content type, etc).

    0 讨论(0)
提交回复
热议问题