简体   繁体   English

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

[英]Error: Download a file from directory using ASP.NET

I'm trying to download a file which has been stored in my HDD.我正在尝试下载已存储在我的硬盘中的文件。 Now i'm tried to use this block of code, and these code are used in button click event.现在我尝试使用这段代码,这些代码用于按钮单击事件。 But the problem is when i'm click the button it download my form(in my case "DAO.aspx") instead of the file (SampleFile.xlsx);但问题是当我单击按钮时,它会下载我的表单(在我的情况下为“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);
        }

    }

Try this:尝试这个:

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