从文件.aspx我需要重定向到网页,打开它然后下载文件。按照我的代码:
页面Source.aspx
<script runat="server">
protected override void OnLoad(EventArgs e)
{
Response.Redirect("Dest.aspx?download=true");
base.OnLoad(e);
}
</script>
页面Dest.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”,而不是我设置的“myFileName.exe”广告 Response.AppendHeader(“content-disposition:”,@“attachment; filename =”“myFileName.exe”“”);
答案 0 :(得分:0)
对于你的第二个问题问题尝试类似的事情
string attachment = string.Format(CultureInfo.InvariantCulture, "attachment; filename=" + sfilename + "", fi.Name);
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", attachment);
希望它适合你。
答案 1 :(得分:0)
RIght,您需要做的是将过程分成两部分:
需要注意的重要一点是,您可以使用网页回复,也可以使用文件回复,而不是同时回复。