views:

342

answers:

3

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.

A: 

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.

klausbyskov
A: 

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.

Stargazer712
With Response.WriteFile, I have an option of assigning the downloadable file name. Ex: Response.AppendHeader("Content-Disposition", "attachment; filename=video.mpg"); I can't do with a reponse.redirect unless I intercept the response and change the file name somehow
+1  A: 

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.

Stargazer712
The architecture will be such that the file is actually stored on the cloud. I would like to have users download directly without going through my web server - this way I optimize bandwidth. The Response.Redirect method works but unfortunately, I can't rename the file (The file is stored with the ID in the name). Any idea how I could do this?
In that case, redirect to a script on the other server that does that for you.The problem is that you are trying to do something that is not allowed by the HTTP protocol (namely, everything must be 1 client request / 1 server response)
Stargazer712
How do I use the Stream object in your response? Can it be used to somehow tie in with the Response.AppendHeader("Content-Disposition", "attachment; filename=video.mpg"); so that I can change the name?