Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Response.WriteFile是否可能包含URL?_C#_Asp.net_Response.write - Fatal编程技术网

C# Response.WriteFile是否可能包含URL?

C# Response.WriteFile是否可能包含URL?,c#,asp.net,response.write,C#,Asp.net,Response.write,我希望能够做到这一点: Response.WriteFile ("http://domain/filepath/file.mpg") 但是,我得到了这个错误: Invalid path for MapPath 'http://domain/filepath/file.mpg' A virtual path is expected. WriteFile方法似乎不适用于URL。有没有其他方法可以将URL的内容写入我的页面 谢谢 基本上你有两个选择。您可以将该文件下载到服务器并使用Respons

我希望能够做到这一点:

Response.WriteFile ("http://domain/filepath/file.mpg")
但是,我得到了这个错误:

Invalid path for MapPath 'http://domain/filepath/file.mpg' 
A virtual path is expected.
WriteFile
方法似乎不适用于URL。有没有其他方法可以将URL的内容写入我的页面


谢谢

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

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

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

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

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

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://domain/filepath/file.mpg");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream file = response.GetResponseStream();
从那时起,您将文件的内容作为一个流,并且必须将字节读/写到响应中

然而,我要提到的是,这并不一定是最优的——您将消耗您的带宽,因为每个文件将使用远远超过必要的资源


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

使用Response.WriteFile,我可以选择指定可下载的文件名。例如:Response.AppendHeader(“内容处置”,“附件;文件名=video.mpg”);我无法使用reponse.redirect,除非我截获响应并更改文件名,WriteFile方法将指定文件的内容作为文件块直接写入HTTP响应输出流。您尚未指定文件的有效路径。架构将使文件实际存储在云上。我想让用户直接下载而不必通过我的web服务器-这样我可以优化带宽。Redirect方法可以工作,但不幸的是,我无法重命名该文件(该文件以名称中的ID存储)。知道我该怎么做吗?在这种情况下,重定向到另一台服务器上为您执行此操作的脚本。问题是,您正在尝试执行HTTP协议不允许的操作(即,所有操作都必须是1个客户端请求/1个服务器响应)。如何在响应中使用流对象?它是否可以用于以某种方式与响应相关联;这样我就可以改名字了?