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.
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.
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.
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
.
精彩评论