Nick
Nick

Reputation: 7525

Response.WriteFile with a URL possible?

I would like to be able to do this:

Response.WriteFile ("http://domain/filepath/file.mpg")

But, I get this error:

Invalid path for MapPath 'http://domain/filepath/file.mpg' 
A virtual path is expected.

The WriteFile method does not appear to work with URLs. Is there any other way I can write the contents of a URL to my page?

Thanks.

Upvotes: 2

Views: 10354

Answers (3)

riwalk
riwalk

Reputation: 14223

If you need the code to work in that manner, then you will have to dynamically download it onto your server first:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://domain/filepath/file.mpg");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream file = response.GetResponseStream();

From that point, you have the contents of the file as a stream, and you will have to read/write the bytes out to the response.

I will mention however, that this is not necessarily optimal--you will be killing your bandwidth, because every file will be using far more resources than necessary.

If possible, move the file to your server, or rethink exactly what you are trying to do.

Upvotes: 3

riwalk
riwalk

Reputation: 14223

A possible solution would be to simply use:

Response.Redirect("http://domain/filepath/file.mpg")

But then, I am not sure if that is what you are really trying to do or not.

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120927

Basically you have a couple of choices. You can either download the file to your server and serve it with Response.WriteFile or you could redirect to the actual location. If the file is already on your server you just have to provide a file system path to Response.WriteFile instead of the url, or use a virtual url, by removing http://domain.

Upvotes: 0

Related Questions