3

Based on this : ASP.Net Download file to client browser

This should also work : ( contenu is a string )

        Response.Clear();

        Response.ClearHeaders();

        Response.ClearContent();

        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + txtFileName.Value.ToString() + "\"");

        Response.AddHeader("Content-Length", contenu.Length.ToString());

        Response.ContentType = "text/plain";

        Response.Flush();

        Response.Write(contenu);

        Response.End();

However, after Response.End() the browser seems to react because i see it working, but then nothing happens...

Same for Chrome and Explorer.

What should i do ?

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • I suspect it may also be something to do with the order of Flush and Write; try swapping their execution ordder – techspider Jan 06 '16 at 17:39

1 Answers1

3

Try this code which has slightly different parameters set; It is working for me

protected void ExportData(string fileName, string fileType, string content)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlPathEncode(fileName));
        Response.Charset = "";
        Response.ContentType = fileType;
        Response.Output.Write(content);
        Response.Flush();
        Response.End();

    }

Call like this

ExportData("Report.txt", "text/plain", yourContentString);
techspider
  • 3,370
  • 13
  • 37
  • 61
  • Ok, i just saw i got fooled by the postbacks : after the function, an accidental postback occurs, so headers where sent but then ignored because of page reload, noob mistake, anyway your function is rock solid so i'll accept your answer – Antoine Pelletier Jan 06 '16 at 18:26