繁体   English   中英

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

[英]Error: Download a file from directory using 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);
        }

    }

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM