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

关于页面有参数时下载文件总是调用迅雷下载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

------解决方案--------------------

//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());