从文件 .aspx 我需要重定向到网页,打开它然后下载文件。按照我的代码:
页面 Source.aspx
<script runat="server">
protected override void OnLoad(EventArgs e)
{
Response.Redirect("Dest.aspx?download=true");
base.OnLoad(e);
}
</script>
页面目标.aspx
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
string download= (string)Request.QueryString["download"];
if (download == "true")
{
string url = "myurl/myfile.exe";
System.Net.HttpWebRequest objRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();
int bufferSize = 1;
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("content-disposition:", @"attachment;filename=""myFileName.exe""");
Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
Response.ContentType = "application/download";
byte[] byteBuffer = new byte[bufferSize + 1];
System.IO.MemoryStream memStrm = new System.IO.MemoryStream(byteBuffer, true);
System.IO.Stream strm = objRequest.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
{
Response.BinaryWrite(memStrm.ToArray());
Response.Flush();
}
Response.Close();
Response.End();
memStrm.Close();
memStrm.Dispose();
strm.Dispose();
}
}
</script>
现在有两个问题:
调用页面“Source.aspx”会自动开始下载,但浏览器中未显示页面 Dest.aspx
下载的文件名为“Dest.aspx”,而不是我使用 Response.AppendHeader("content-disposition:", @"attachment;filename=""myFileName.exe"""); 设置的“myFileName.exe”广告。