作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
<分区>
我正在使用 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();
这是提示用户下载时的屏幕:
这是用户选择“打开”后的屏幕
这是用户选择“重试”后的屏幕。此屏幕适用于 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);
}
我是一名优秀的程序员,十分优秀!