Response.WriteFile 可以带有 URL 吗?

发布于 2024-08-19 19:43:35 字数 315 浏览 3 评论 0原文

我希望能够执行此操作:

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

但是,我收到此错误:

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

WriteFile 方法似乎不适用于 URL。有没有其他方法可以将 URL 的内容写入我的页面?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独患者 2024-08-26 19:43:35

如果您需要代码以这种方式工作,那么您必须首先将其动态下载到您的服务器上:

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

从那时起,您将文件的内容作为流,并且您必须读取/写入字节到响应。

然而,我要提到的是,这不一定是最佳的——您将耗尽带宽,因为每个文件将使用比必要的更多的资源。

如果可能,请将文件移动到您的服务器,或者重新考虑您正在尝试执行的操作。

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.

暖风昔人 2024-08-26 19:43:35

一个可能的解决方案是简单地使用:

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

但是,我不确定这是否是您真正想要做的。

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.

时光瘦了 2024-08-26 19:43:35

基本上你有几个选择。您可以将文件下载到服务器并使用 Response.WriteFile 提供服务,也可以重定向到实际位置。如果该文件已在您的服务器上,您只需提供 Response.WriteFile 的文件系统路径而不是 url,或者通过删除 http://domain< 来使用虚拟 url /代码>。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文