日期:2014-05-17  浏览次数:20504 次

关于页面有参数时下载文件总是调用迅雷下载aspx页面
我的下载方法如下:
public static void down(string filePath)
  {
  FileInfo DownloadFile = new FileInfo(filePath);
  HttpContext.Current.Response.Clear();
  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, Encoding.UTF8));
  HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
  HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
  HttpContext.Current.Response.Flush();
  HttpContext.Current.Response.End();
  }
方法本身没有问题,如果这个页面没有参数,直接调用就没事,即使调用迅雷也是下载原来的文件,但如果页面有参数,就会导致调用迅雷把这个页面给下载下来,后缀是.aspx
找了半天原因才发现是页面参数捣的鬼,问下各位高手有什么解决办法么?我不想用a连接或这直接Response.Redirect

------解决方案--------------------
C# code

//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/z.zip");
Response.TransmitFile(filename);
}
//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
string fileName ="asd.txt";//客户端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.txt");//路径
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
//WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}

------解决方案--------------------
方法一:点击保存后在弹出页面选择使用IE下载
方法二: 把迅雷中的设置--高级 --监视浏览器的勾去掉

再有就是试试压缩下载看行不行
------解决方案--------------------
HTML code
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnExport" runat="server" Text="导出测试" 
            onclick="btnExport_Click" />
    </div>
    </form>
</body>