Asp.net 下载指定流中的文件?
我在一个Asp.NETMVCWeb应用程序中 对于特定操作,我必须返回从另一个内部服务器生成的文件。此服务器需要一些web用户不拥有的凭据。因此Asp.net MVC服务器必须将自身连接到此服务器,下载并将下载的文件发送到客户端 实际上我是通过一个ActionResult来实现的:Asp.net 下载指定流中的文件?,asp.net,asp.net-mvc,reporting-services,stream,httpwebrequest,Asp.net,Asp.net Mvc,Reporting Services,Stream,Httpwebrequest,我在一个Asp.NETMVCWeb应用程序中 对于特定操作,我必须返回从另一个内部服务器生成的文件。此服务器需要一些web用户不拥有的凭据。因此Asp.net MVC服务器必须将自身连接到此服务器,下载并将下载的文件发送到客户端 实际上我是通过一个ActionResult来实现的: public class ReportServerFileResult : FileResult { private readonly string _host; privat
public class ReportServerFileResult : FileResult
{
private readonly string _host;
private readonly string _domain;
private readonly string _userName;
private readonly string _password;
private readonly string _reportPath;
private readonly Dictionary<string, string> _parameters;
/// <summary>
/// Initializes a new instance of the <see cref="ReportServerFileResult"/> class.
/// </summary>
/// <param name="contentType">Type of the content.</param>
/// <param name="host">The host.</param>
/// <param name="domain">The domain.</param>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The password.</param>
/// <param name="reportPath">The report path.</param>
/// <param name="parameters">The parameters.</param>
public ReportServerFileResult(string contentType, String host, String domain, String userName, String password, String reportPath, Dictionary<String, String> parameters)
: base(contentType)
{
_host = host;
_domain = domain;
_userName = userName;
_password = password;
_reportPath = reportPath;
_parameters = parameters;
}
#region Overrides of FileResult
/// <summary>
/// Writes the file to the response.
/// </summary>
/// <param name="response">The response.</param>
protected override void WriteFile(HttpResponseBase response)
{
string reportUrl = String.Format("http://{0}{1}&rs:Command=Render&rs:Format=PDF&rc:Toolbar=False{2}",
_host,
_reportPath,
String.Join("", _parameters.Select(p => "&" + p.Key + "=" + p.Value)));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reportUrl);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(_userName, _password, _domain);
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
Stream fStream = webResponse.GetResponseStream();
webResponse.Close();
fStream.CopyTo(response.OutputStream);
fStream.Close();
}
#endregion
}
但我不喜欢流的管理方式,因为如果我理解正确,我们会检索文件的完整流,然后开始将其发送给最终用户
但这意味着我们要在内存中加载完整的文件,不是吗
因此,我想知道是否有可能向HttpWebRequest提供它必须在其中写入响应的流?要将web响应直接流式传输到输出
可能吗?你有其他想法吗?你已经在连接这两条流了。仅仅因为调用GetResponse,就不会将整个数据读取到内存中 当目标流可以接受写入时,CopyTo应该根据需要从源流读取数据,具体取决于目标流的传输速度
您是否有任何指标表明整个内容被读取到web进程的内存中?没有,我只是认为GetResponseStream将url的内容放入memoryStream。请使用调试器进行检查-我99%确定返回的流是某种网络流,而不是内存流。