在IE9中选择"打开"时,Asp.Net下载文件错误

Eri*_* R. 9 c# asp.net internet-explorer download

我正在使用Response让我的应用程序为用户打开Word文档.如果用户选择保存文件,它会保存文件并且文件看起来在打开时应该如何保存.如果用户选择立即打开文件,则会收到错误消息,指出IE无法打开该文件.如果他们选择"重试",则MS Word会显示错误消息,指出无法找到该文件.以下是显示我的情况的屏幕截图.此外,这是我必须显示文件的代码:

        this.Context.Response.Clear();
        this.Context.Response.ClearContent();
        this.Context.Response.ClearHeaders();
        this.Context.Response.BufferOutput = true;
        this.Context.Response.ContentType = "application/msword";
        this.Context.Response.AppendHeader("Content-Length", bytes.Length.ToString()); 
        this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
        this.Context.Response.BinaryWrite(bytes);
        this.Context.ApplicationInstance.CompleteRequest();
Run Code Online (Sandbox Code Playgroud)

这是提示用户下载时的屏幕: 在此输入图像描述

这是用户选择"打开"后的屏幕 在此输入图像描述

这是用户选择"重试"后的屏幕.这个屏幕即将推出MS Word. 在此输入图像描述

****编辑****我在网上发现了一些代码,当我调用此函数时,我尝试测试并发出问题:

    protected void GenerateMsWordDoc()
    {
        string strBody = "<html>" +
            "<body>" + 
                "<div>Your name is: <b>Billy Bob</b></div>" +
                "<table width='100%' style='background-color:#cfcfcf;'><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
                "Ms Word document generated successfully." +
            "</body>" +
            "</html>";
        string fileName = "MsWordSample.doc";
        // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
        Response.Write(strBody);
    }
Run Code Online (Sandbox Code Playgroud)

Pan*_*kaj 1

您可以发布正在使用的示例数据吗?我在 IE9 中尝试了下面的代码,效果很好。

this.Context.Response.Clear();
this.Context.Response.ClearContent();
this.Context.Response.ClearHeaders();
this.Context.Response.BufferOutput = true;
this.Context.Response.ContentType = "application/msword";
this.Context.Response.AppendHeader("Content-Length", "12");
this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
this.Context.Response.BinaryWrite(new byte[] { });
this.Context.ApplicationInstance.CompleteRequest();
Run Code Online (Sandbox Code Playgroud)

您最近的代码也运行良好。我用的是IE9。以下是版本详细信息...

在此输入图像描述