How is an image served from a URL with an ASPX extension? - c#

Can Somebody tell how to create such kind of urls
for example if you see the url
http://office.microsoft.com/global/images/default.aspx?assetid=ZA103873861033
you will redirect to an image ..
my question is , though this url is an image..its extension is aspx..how is it possible.
how to create such kind of url's
Thanks

This is a common method for displaying an image that's stored as a binary object in a database. One tutorial, among many, can be found here.
Essentially, what they're doing is using the aspx page to accept the URL parameter which tells them what image to fetch from the database. Then in the response they clear all output and headers, set the headers for the image, write the binary data to the response stream, and close the response stream.
So it's not really "redirecting" you to an image. The "page" being requested turns out to be an image resource in the response.

By setting the ContentType in the response from the server
HttpContext.Response.ContentType = "image/jpeg";

easiest way is to add generic handler *.ashx and in ashx file u'll have code behind which u can get querystring and manipulate response eg. Response.WriteFile(...)

File extensions literally have no meaning on the WWW. The thing that correctly describes the content at a particular URL is the content-type/MIME-type. This is delivered in an HTTP header when the URL is requested prior to delivery of the main HTTP payload. Other answers describe how you might correctly set this in ASP.NET.

Aside from all other answers they may be doing a Server.Transfer() (so that you don't see it client-side) to the image file. This still means the response headers are being set to the appropriate MIME type but it also means the image isn't necesarilly coming from a database. This technique can be used to hide the actual image URL in attempts to prevent hotlinking.

Related

How can I get the source of an iframe that only works on specified domains?

So I'm trying to read the source of an url, let's say domain.xyz. No problem, I can simply get it work using HttpWebRequest.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
My problem is that it will return the page source, but without the source of the iframe inside this page. I only get something like this:
<iframe src="http://anotherdomain.xyz/frame_that_only_works_on_domain_xyz"></iframe>
I figured out that I can easily get the src of the iframe with WebBrowser, or basic string functions (the results are the same), and create another HttpWebRequest using the address. The problem is that if I view the full page (where the frame was inserted) in a browser (Chrome), i get the expected results. But if I copy the src to another tab, the contents are not the same. It says that the content I want to view is blocked because it's only allowed through domain.xyz.
So my final question is:
How can I simulate the request through a specified domain, or get the full, rendered page source?
That's likely the referer property of the web request: typically a browser tells the web server where it found the link to the page it is requesting.
That means, when you create the web request for the iframe, you set the referer property of that request to the page containing the link.
If that doesn't work, cookies may be another option. I.e. you have to collect the cookies sent for the first request, and send them with the second request.

pdf.js how to pass a file response object in c#

so I am using the excellent pdf.js tool and it works great. However, I'd like to pass it a response object that has the PDF file stream from Amazon S3 over to the viewer of pdf.js.
In the demo I see it calls it like this:
=/pdf/web/viewer.html?file=%2FmypdfFile.pdf
However, looking in viewer.html or pdf.js or any of its files, I cannot see where on earth its using the ?file parameter that is passed on the URL. I'd like to replace it with something where I can pass it a response item and it will load up the viewer.html.
I'd like to do something like this:
(pseudo code sorta)
request = S3.GetObjectRequest(bucket, key);
using GetObjectResponse response - client.getobject(request);
openPDFViewer (response);
Is that doable? response would contain the file, i.e. I can say
response.WriteResponseToFile("c:\mypdf.pdf")
and I get the file out.
I cannot see where on earth its using the ?file parameter that is
passed on the URL. I'd like to replace it with something where I can
pass it a response item and it will load up the viewer.html.
If you look into viewer.js, there is this pdfViewOpen method having following parameter:-
pdfViewOpen(url, id, scale, password,pdfDataRangeTransport, args)
The string url that you pass after file= is passed in as url parameter of this method. You can change inside that method what u want to do with it.
From there you might want to look at PDFJS.getDocument() method of pdf.js file, this follows to fetchDocument method from where its taken care by messageHandler.
If you want to handle all that by yourself, you can intercept it before. There are already several examples on SO. Here is link to one:-
How to get byte-array data from servlet to pdf.js
IIRC S3 files have URL references so you don't have to 'preload' the pdf on the server side. Just render out the URL to have the file=[urlencoded path to s3 file]

Setting up application to download a file in asp.net MVC - Nothing happening on return from download

I realize that variations on this question have been asked before. The best answer I have found is at
File download in Asp.Net MVC 2
But trying to follow those instructions did not solve the problem for me.
Long story short, the file is being retrieved correctly, the name, path and mime type are all correct, and no errors are thrown. No errors are thrown by the javascript on the client-side either.
The C# code that gets the file looks like this:
[HttpPost]
public FileResult DownloadFile(int fileId)
{
... get the file and file info
return File(fileBytes, fileMimeType, fileName);
}
The javascript looks like this:
... set up for post here
$.post(settings.actions.downloadFile, {fileId: fileIdVar});
As I was saying, the post returns and nothing happens.
I have tried changing the post to a get, and the result was the same.
I have tried setting up a callback function that sets document.location.href to some random url on return from the download, but that just takes my browser to the page I specified. I cannot understand, from the explanation given in the link I provided, that is
"...Use document.location.href = ... to tell the browser to go to the url for downloading the file. It'll see the content disposition header and will display it as a download not as a page..."
What I'm supposed to point my browser to. document.location.href doesn't accept data, so I can't use it on its own, and using post without document.location.href returns nothing.
What could I be doing wrong?
Big thanks to responders for their time!
Just like the answer in the post you linked says, you can't download a file via AJAX.
In order to set the location, change your action to respond to GET requests and either add your file id to the query string or setup a route to handle it. Also, and you may already be doing this, but you'll need to make sure you set the Content-Dispostion header value to attachment.
window.location.href = settings.action.downloadFile + "?fileId=" + fileIdVar
Since you are using jQuery, you could use $.param to build the parameters for you.
You could also look into a plugin to provide an "AJAX like" experience.

How to get the address of a redirected page?

The goal of my program is to grab a webpage and then generate a list of Absolute links with the pages it links to.
The problem I am having is when a page redirects to another page without the program knowing, it makes all the relative links wrong.
For example:
I give my program this link: moodle.pgmb.si/moodle/course/view.php?id=1
On this page, if it finds the link href="signup.php" meaning signup.php in the current directory, it errors because there is no directory above the root.
However this error is invalid because the page's real location is:
moodle.pgmb.si/moodle/login/index.php
Meaning that "signup.php" is linking to moodle.pgmb.si/signup.php which is a valid page, not moodle.pgmb.si/moodle/course/signup.php like my program thinks.
So my question is how is my program supposed to know that the page it received is at another location?
I am doing this in C Sharp using the follownig code to get the HTML
WebRequest wrq = WebRequest.Create(address);
WebResponse wrs = wrq.GetResponse();
StreamReader strdr = new StreamReader(wrs.GetResponseStream());
string html = strdr.ReadToEnd();
strdr.Close();
wrs.Close();
You should be able to use ResponseUri method of WebResponse class. This will contain the URI of the internet resource that actually provided the response data, as opposed to the resource that was requested. You can then use this URI to build correct links.
http://msdn.microsoft.com/en-us/library/system.net.webresponse.responseuri.aspx
What I would do is first check if each link is absolute or relative by searching for an "http://" within it. If it's absolute, you're done. If it's relative, then you need to append the path to the page you're scanning in front of it.
There are a number of ways you could get the current path: you could Split() it on the slashes ("/"), then recombine all but the last one. Or you could search for the last occurrence of a slash and then take a substring of up to and including that position.
Edit: Re-reading the question, I'm not sure I am understanding. href="signup.php" is a relative link, which should go to the /signup.php. So the current behavior you mentioned is correct "moodle.pgmb.si/moodle/course/signup.php."
The problem is that, if the URL isn't a relative or absolute URL, then you have no way of knowing where it goes unless you request it. Even then, it might not actually be being served from where you think it is located. This is because it might actually be implemented as a HTTP Redirect or similar server side.
So if you want to be exhaustive, what you can do is:
Use your current technique to grab a list of all links on the page.
Attempt to request each of those pages. Then if you:
Get a 200 responce code then all is good - it's there.
Get a 404 response code you know the page does not exist
Get a 3XX response code then you know where the web server
expects that content to actually orginate form.
Your (Http)WebResponse object should have a ResponseCode property. Note that you should also handle any possible WebException errors - these too will have a WebResponse with a ResponseCode in (usually 5xx).
You can also look at the HttpWebResponse Headers property - the Location header.

I want the user to be able to download a page

I am using the code,
string loadFile = HttpContext.Current.Request.Url.AbsoluteUri;
// this.Response.ClearContent();
// this.Response.ClearHeaders();
this.Response.AppendHeader("content-disposition", "attachment; filename " + filename);
this.Response.ContentType ="application/html";
this.Response.WriteFile("C:\\Users\\Desktop\\Jobspoint Website\\jobpoint3.0\\print.aspx");
this.Response.Flush();
this.Response.Close();
this.Response.End();
to download an aspx page in asp.net C#.. But its only showing the html tags and static values... How can I save the entire page without html tags and with the values that retrieved from the database?
Thanks...
Leema
Use WebClient for this. It will download your file.
If I have understood correctly, one option would be to actually make a request to the web server using WebClient for example. And then write the response to that request to the Response.OutputStream. This means that the server will actually make a second request to it self and then send the response to the second request back to the client.
This way you will have the web server actually process the request and return the resulting HTML back to you rather than just the raw aspx page.

Categories

Resources