私は Asp.Net MVC Web アプリケーションを使用しています。
特定のアクションのために、別の内部サーバーから生成されたファイルを返す必要があります。このサーバーには、Web ユーザーが所有していない資格情報が必要です。したがって、Asp.net MVC サーバーは、このサーバーに接続し、ダウンロードしたファイルをダウンロードしてクライアントに送信する必要があります。
実際、私は ActionResult を通してそれをやっています:
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 応答を出力に直接ストリーミングするには?
出来ますか?別のアイデアはありますか?