2

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.

Nick
  • 7,475
  • 18
  • 77
  • 128
  • The WriteFile method writes the contents of the specified file directly to an HTTP response output stream as a file block. You have not specified a valid path to a file. – DaveB Feb 03 '10 at 18:35

3 Answers3

3

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.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • 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? – Nick Feb 03 '10 at 22:22
  • 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) – riwalk Feb 03 '10 at 22:47
  • 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? – Nick Feb 03 '10 at 23:01
1

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.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • 2
    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 – Nick Feb 03 '10 at 18:34
0

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.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222