如何在ASP.NET C#中打开文件(pdf或jpg)后不更改网页中的字体?
可能重复:如何在ASP.NET C#中打开文件(pdf或jpg)后不更改网页中的字体?,c#,asp.net,file,fonts,C#,Asp.net,File,Fonts,可能重复: 之前我发布了这个问题: 事实上,我已经问过这个问题了,但这只是一个小问题,所以我想在上一篇文章中它并没有那么重要,所以我会在这里问 每当我打开pdf时,使用: Response.Write("<script>window.open('FilePath');</script>"); Response.Write(“window.open('FilePath');”; 页面中的所有字体都被更改,例如,字母的大小增加,字母的一些颜色被切换为黑色,而不是我指定
之前我发布了这个问题: 事实上,我已经问过这个问题了,但这只是一个小问题,所以我想在上一篇文章中它并没有那么重要,所以我会在这里问 每当我打开pdf时,使用:
Response.Write("<script>window.open('FilePath');</script>");
Response.Write(“window.open('FilePath');”;
页面中的所有字体都被更改,例如,字母的大小增加,字母的一些颜色被切换为黑色,而不是我指定的字体
有没有办法让我解决这个问题
哦,我注意到,当你打开像jpg这样的图片时,也会发生这种情况。我不确定直接用响应来写。在这种情况下,写是最好的解决方案。我的记忆对这一点很有价值,但似乎我(很久以前)尝试通过响应直接写入输出。写入并遇到了一些像您描述的奇怪行为。其他SO成员应该能够更好地解释为什么会发生这种情况,但我认为这与您在页面生命周期和IIS流中的位置有关,即Response.Write是否能够实现所需的行为。如果你想直接把文件推到客户端,你可以考虑MVC。MVC使得通过FilePathResult类将文件直接推送到客户机变得更加容易。如果愿意,您可以混合和匹配MVC和WebForms。斯科特·汉斯勒曼展示了如何做到这一点 但是,如果您想继续使用WebForms,那么我将提供一个简单的页面,演示如何使用RegisterStartupScript通过单击相应的按钮打开两个本地PDF中的任意一个: ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Register Startup Script Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="bnOpenTest1PDF" runat="server"
OnClick="opnTest1PDF" Text="OpenPDF1" />
<asp:Button ID="bnOpenTest2PDF" runat="server"
OnClick="opnTest2PDF" Text="OpenPDF2" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void opnTest1PDF(Object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"myFileOpenScript",
"<script>window.open('test1.pdf');</script>");
}
protected void opnTest2PDF(Object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"myFileOpenScript",
"<script>window.open('test2.pdf');</script>");
}
}
注册启动脚本示例
代码隐藏:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Register Startup Script Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="bnOpenTest1PDF" runat="server"
OnClick="opnTest1PDF" Text="OpenPDF1" />
<asp:Button ID="bnOpenTest2PDF" runat="server"
OnClick="opnTest2PDF" Text="OpenPDF2" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void opnTest1PDF(Object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"myFileOpenScript",
"<script>window.open('test1.pdf');</script>");
}
protected void opnTest2PDF(Object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"myFileOpenScript",
"<script>window.open('test2.pdf');</script>");
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的void optest1pdf(对象发送方,事件参数e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
“myFileOpenScript”,
“window.open('test1.pdf');”;
}
受保护的void optest2pdf(对象发送方,事件参数e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),
“myFileOpenScript”,
“window.open('test2.pdf');”;
}
}
再次打开同一个问题无助于解决问题!如果尝试RegisterStartupScript而不是响应,您可能会得到更好的结果;