简体   繁体   中英

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

    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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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