错误:使用ASP.NET从目录下载文件

时间:2015-11-14 18:23:14

标签: c# asp.net

我试图下载存储在硬盘中的文件。现在我尝试使用这段代码,这些代码用于按钮点击事件。 但问题是当我点击它下载我的表格的按钮(在我的情况下" DAO.aspx")而不是文件(SampleFile.xlsx);

    protected void BtnDownload_Click(object sender, EventArgs e)
    {
        try
        {                
            Response.Clear();
            Response.ContentType = "application/octect-stream";
            Response.AppendHeader("content-disp­osition", "attachment; filename=SampleFile.xlsx");
            Response.TransmitFile(Server.MapPath("~/SampleExcel/SampleFile.xlsx"));
            Response.End();

        }
        catch (Exception ex)
        {               
            MessageBox.Show(ex.Message);
        }

    }

1 个答案:

答案 0 :(得分:1)

试试这个:

Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "Application/octect-stream";
Response.AppendHeader("Content-disposition", "attachment; filename=SampleFile.xlsx");
Response.WriteFile(Server.MapPath("~/SampleExcel/SampleFile.xlsx"));