Find Out the URL of the webpage that image resides on - c#

I would like to make a 1 pixel image that will reside on the html page to track page activity
I have a page http:/domain.com/mypage.htm
and I want to add <img src='http://www.test.com/myimage.aspx' /> somewhere in its body
When page is triggered I render an one pixel transparent image. I'm having an issue finding out the URL of the page that image is on. HTTP_REFERER header carrier the true referer of the page, and HTTP_HOST carries the URL of the image itself.
Is there a way to find out the HTML page URL that the image is on?

If you can't find it on any http header, you can always send some information like <img src='http://www.test.com/myimage.aspx?page=mypage' />.
If you can use javascript though, you don't need to display an image, use an ajax request instead.

You can analysis the request headers of myimage.aspx, the Referer header is the HTML page URL you're looking for. Besides, I suggest adding a timestamp with the <img> src link, like this:
<img src="http://www.test.com/myimage.aspx?t=1234567890" />
So that every time you refresh the HTML page, the browser invoke a new request to the image.

Related

Displaying images from database using c# .net

recently, I've been developing a website with .NET c# and I've been trying to display images via the imagepath in the db. It is currently working by this line of code.
return File(byte array, "image/jpeg");
The problem is that by this line of code the layout page is completely ignored as only the image with white background is shown. I need to display the image along with the return view (the image inside the layout of the website). thx
The reason you're not seeing anything is because you can only have one type of response per request; Either a response containing HTML or a response containing the image.
What you need to do in order to serve images via your application is set up a route that you can put in your <img> tags as the src attribute.
<img src="/static/images/123">
Your route would listen for requests on the /static/images/ path and then try to parse the ID number at the end. It could then take that ID number (123) and look up the relevant image in your database.
So, to be clear, you'd have at least two requests that occur; First, you serve the request for the page, then you serve subsequent requests for the image(s). These two request handlers do not share the same code.
Finally, if you really wanted to "inline" an image as part of the page response, The only way you can "inline" an image into a page is to base64 encode it and set that as as the src of an <img> tag. This process is slow and bloats your HTML, making it take longer to load.

Display varbinary as a downloadable link

Fairly straightforward question, I have images stored in my database as varbinary and would like to provide a link to these images rather than displaying them on the website. When a user clicks the link he/she should be able to download the image.
An image is served in response to a request.
<img src="?" /> <!-- what goes here? -->
You need to create an HTTP handler to receive requests for these images.
A handler is an executable available at a specific URL which can respond to your request; in this case, by serving the binary data of an image. An ASPX page is a valid handler, though there are more efficient handler types for images.
<img src="MyHandler.aspx?imageId=123" />
The handler should do a few things:
validate that the ID is valid and that the caller has permissions (if needed)
retrieve the image from the database
set appropriate response headers
use Response.BinaryWrite() to send the binar data to the client.
Note that if you are using ASP.Net MVC, you can use a controller as your handler.
An alternative method is to base 64 encode the bytes and embed them directly in the image tag.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This is a useful technique for small images or when the expense of multiple requests is very high.
See: http://en.wikipedia.org/wiki/Data_URI_scheme
More reading:
Dynamically Rendering asp:Image from BLOB entry in ASP.NET
Dynamic Image Generation
You should create a page that takes an image-id and returns the image.
You may consider this question:
Display image from a datatable in asp:image in code-behind
I had a similar problem an that answer solved my question. Beside that, you can use binaryimage component from Telerik:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

HttpHandler not firing from body of .aspx page

I have a http handler that is called from a .aspx page in the form 1x1 pixel image. The handler has an extension of .jpg set up in the web.config. On all browsers apart from IE the http handler is called successfully from the body of the page when it loads.
However in IE the httphandler is not called.
If I call the http handler by entering the url in to the address bar in IE it works perfectly.
Any idea why it might not work in the body of the page?
IE may have it cached the image. If the filename of the .jpg isn't changing, I'd try to add a random query string to the end of it.
http://<path>/0104924934404624104049.jpg?random=<unix timestamp>

HttpWebRequest reades only homepage

Hi I tried to read a page using HttpWebRequest like this
string lcUrl = "http://www.greatandhra.com";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
mydiv.InnerHtml = lcHtml;
// Response.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();
i can able to read that page and bind it to mydiv. But when i click on any one of links in that div it is not displaying any result. Because my application doesnt contain entire site. So what we will do now.
Can somebody copy my code and test it plz
Nagu
I'm fairly sure you can't insert a full page in a DIV without breaking something. In fact the whole head tag may be getting skipped altogether (and any javascript code there may not be run). Considering what you seem to want to do, I suggest you use an IFRAME with a dynamic src, which will also hopefully lift some pressure off your server (which wouldn't be in charge of fetching the html to be mirrored anymore).
If you really want a whole page of HTML embedded in another, then the IFRAME tag is probably the one to use, rather than the DIV.
Rather than having to create a web request and have all that code to retrieve the remote page, you can just set the src attribute of the IFRAME to point ot the page you want it to display.
For example, something like this in markup:
<iframe src="<%=LcUrl %>" frameborder="0"></iframe>
where LcUrl is a property on your code-behind page, that exposes your string lcUrl from your sample.
Alternatively, you could make the IFRAME runat="server" and set its src property programatically (or even inject the innerHTML in a way sismilar to your code sample if you really wanted to).
The code you are putting inside .InnerHtml of the div contains the entire page (including < html >, < body >, < /html > and < /body> ) which can cause a miriad of problems with any number of browsers.
I would either move to an iframe, or consider some sort of parsing the HTML for the remote site and displaying a transformed version (ie. strip the HTML ,BODY, META tags, replace some link URLs, etc).
But when i click on any one of links in that div it is not displaying any result
Probably because the links in the download page are relative... If you just copy the HTML into a DIV in your page, the browser considers the links relative to the current URL : it doesn't know about the origin of this content. I think the solution is to parse the downloaded HTML, and convert relative URLs in href attributes to absolute URLs
If you want to embed it, you need to strip everything but the body part. That means that you have to parse your string lcHTML for <body....> and remove everything before and includeing the body tag. You must also strip away everything from </body>. Then you need to parse the string for all occurences of <a href="....."> that do not start with http:// and include h t t p://www.greatandhra.com or set <base target="h t t p://www.greatandhra.com"> in your head section.
If you don't want to embed, simply clear the response buffer and stream the lcHTML string back to the browser.
PS: I had to write all h t t p with spaces to be able to post this.
Sounds like what you are trying to do is display a different site embedded in your site. For this to work by dropping it into a div you would have to extract the code between the body tags as it wouldn't be valid with html and head in the middle of another page.
The links won't work because you've now taken that page out of context in your site so you'd also have to rewrite any links on the page that are relative (i.e. don't start with http) to point to a page on your site which will then fetch the other sites page and display them back in your site, or you could add the url of the site you're grabbing to the beginning of all the relative links so they link back to that site.

Why doesn't the image load when dynamically loading css in asp.net?

I am loading a css file dynamically by making the link style attribute a server tag.
All of the css loads fine except for the image. It just shows the alternate text. I am doing this in the page_load event.
Here is a snippet of my img markup:
<img class="logourl" alt="Header" />
Here is a the css for logourl:
.logourl
{
background-image:url(../images/a-logo.png);
width:169px;
height:61px;
margin-top:5px;
}
When I right-click on the image and view properties, it is blank (size, address, etc).
<img class="logourl" alt="Header" />
Can an <img> tag have a background-image property???? That doesn't even make sense.
According to MSDN, the img object doesn't have this property, so I'd say it's safe to assume it's not supported in IE.
Are you sure you don't want a span, a hyperlink, or some other property where a background-image would make sense?
edit
I just read #Justin Grant's answer. I guess that you can use a background image on an image tag, but I'm keeping my answer because it seems a silly way to do it. if what you want is a frame around your image, I would create a div or span with a set width and height, and an img slightly smaller inside it.
I wouldn't call that loading the CSS file dynamically. You are setting the attribute in the link tag dynamically, but the CSS file is loaded the same way as any other CSS file. The browser doesn't see any difference from any other link tag, so the dynamically set url is not part of the problem.
Where are the image and the css files located? Remember that the url is relative to where the CSS file is, not where the page is.
Note that even if you mangage to set the background image for the image, it will still show the alt text on top of the image and indicate that the image is not loaded. The background-image attribute does not set the source of the image. You should just use a div element instead of the image element.
To put a background-image on an IMG tag, according to http://www.contentwithstyle.co.uk/content/css-background-image-on-html-image-element you'll need to apply a CSS padding and make sure you're using display:block.
Also, if this is a directory-path issue, the image must be relative to the location of the CSS file it's referenced from. Try using an absolute path to verify that this is indeed the problem, then experiment with various relative paths to see if that it gets solved.
If it's still unsolved at that point, I'd suggest trying Fiddler (www.fiddlertool.com) which will let you see HTTP requests being made-- specifically the actual URL being requested by your browser. Make sure to clear your cache first before reloading the page under Fiddler, so you'll be sure to actually make the request and not pull from cache. Anyway, there will be three possible cases:
Fiddler does not show you requesting a URL at all. This points to some issue with your CSS (e.g. not being loaded properly, or a syntax error)
Fiddler shows the image URL being requested, but it shows a 404 (not found) or other error. this means the wrong URL is being requested, or there's a problem with your web site's serving of images from that folder
Fiddler shows the correct URL, and it's getting a 200 status (success) from the server. If this happens, I'm stumped!
Right-click will only show you SRC of an IMG tag. when using a background CSS, you'll need view source.

Categories

Resources