Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

My requirement was to download a file from my project folder.. i wrote the following code:



C#
protected void btnOpenSampleExcel_Click(object sender, EventArgs e)
{
    try
    {
        string fileName = "SampleRetsServerCredentials.csv";
        string filePath = Server.MapPath("/SampleRetsCredential\\SampleRetsServerCredentials.csv");
        Response.AppendHeader("content-disposition", "filename=" + fileName);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath, false);
    }

    catch (Exception ex)
    {
        _alertMessage = "Sorry, cant show the sample file now! Please try after some time.";
        radManager.RadAlert("message", 400, null, "title", null);
    }
}





Download is working correctly, but the Master page header portion is also shown in the csv after downloading.. How can i prevent such a behaviour.

Thanks,
Amrutha.
Posted
Updated 27-May-14 0:14am
v3

1 solution

Hi,

Just clear your response before write the file stream.

e.g.

C#
Response.Clear();
Response.AppendHeader("content-disposition", "filename=" + fileName);
Response.ContentType = "application/octet-stream";
Response.WriteFile(filePath, false);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900