Antoine Pelletier
Antoine Pelletier

Reputation: 3316

Download File to Client Browser, stuck at Response.End()

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 ?

Upvotes: 3

Views: 4701

Answers (1)

techspider
techspider

Reputation: 3410

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

Upvotes: 3

Related Questions