将文件下载到客户端浏览器,卡在Response.End()

时间:2016-01-06 16:53:53

标签: c# asp.net browser download

基于此:ASP.Net Download file to client browser

这也应该有效:(contenu是一个字符串)

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

然而,在Response.End()之后,浏览器似乎做出了反应,因为我看到它正常工作,但后来没有任何反应......

Chrome和资源管理器相同。

我该怎么办?

1 个答案:

答案 0 :(得分:2)

尝试使用稍微不同的参数设置的代码;这对我有用

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

    }

像这样打电话

ExportData("Report.txt", "text/plain", yourContentString);