在ASP中打开一个文件(pdf或jpg)后如何不改变字体。净c#吗?(复制)

时间:2021-09-07 21:08:11

Possible Duplicate:
How to not alter font in webpage after opening a pdf in ASP.NET C#?

可能的重复:如何不改变在网页中打开一个pdf在ASP。净c#吗?

Previously I posted this question:

之前我发布了这个问题:

How to open files from a specific route in ASP-NET c#?

如何在asp.net c#中从特定路径打开文件?

in fact, I have already asked this however it was only a minor question so I guess it wasn't that important in the previous post, so I will ask here.

事实上,我已经问过这个问题了,但这只是一个小问题,所以我想在之前的文章中,它并不是那么重要,所以我在这里问一下。

Whenever I open a pdf with:

当我以下列方式开启pdf文件:

Response.Write("<script>window.open('FilePath');</script>");

All of the font in the page is altered, example, the letter's size increases and some of the letter's colors are switched to black instead of the font that I assigned.

页面中的所有字体都被修改了,例如,字母的大小增加了,一些字母的颜色被换成了黑色,而不是我指定的字体。

Is there a way that I can work around that??

有什么办法可以解决这个问题吗?

http://imageshack.us/a/img838/5145/beforeja.png

http://imageshack.us/a/img838/5145/beforeja.png

http://imageshack.us/a/img546/4760/afterw.png

http://imageshack.us/a/img546/4760/afterw.png

Oh and I noticed that this also happens when you open images like jpg

我注意到当你打开像jpg这样的图片时也会发生这种情况

1 个解决方案

#1


1  

I'm not sure writing directly with Response.Write is the best solution in this particular case. My memory is vauge on this but it seems that I tried (a long time ago) writing directly to the output via Response.Write and ran into some odd behavior like you described. The other SO members should be able to better elaborate why this happens but I think it has a bit to do with where you are in the page lifecycle and IIS Stream as to whether or not Response.Write will achive the desired behavior. If you want to push files out directly to the client, you might consider MVC. MVC makes it much easier to push a file out directly to the client by way of the FilePathResult Class. You can mix and match MVC and WebForms if you like. Scott Hansleman shows how to do this here.

我不确定是直接写回应。在这个特殊的情况下,写是最好的解决方案。我对这一点记忆深刻,但似乎(很久以前)我尝试过通过响应直接写入输出。像你描述的那样,写作和遇到一些奇怪的行为。另一个SO成员应该能够更好地解释为什么会发生这种情况,但是我认为这与您在页面生命周期和IIS流中的位置有关,至于是否响应。写作能达到预期的行为。如果您想将文件直接推送到客户端,可以考虑使用MVC。MVC使得通过FilePathResult类将文件直接推到客户端更加容易。如果您愿意,您可以混合和匹配MVC和WebForms。Scott Hansleman在这里展示如何做到这一点。

If, however, you want to stick with WebForms, then here is a simple page I have thrown together to show how you can use RegisterStartupScript to open either of two local PDFs by clicking the respective button:

但是,如果您想要继续使用WebForms,那么这里有一个简单的页面,我把它放在一起,向您展示如何使用RegisterStartupScript来通过单击相应的按钮来打开本地的两个pdf文件:

ASPX:

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>

Code Behind:

背后的代码:

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

#1


1  

I'm not sure writing directly with Response.Write is the best solution in this particular case. My memory is vauge on this but it seems that I tried (a long time ago) writing directly to the output via Response.Write and ran into some odd behavior like you described. The other SO members should be able to better elaborate why this happens but I think it has a bit to do with where you are in the page lifecycle and IIS Stream as to whether or not Response.Write will achive the desired behavior. If you want to push files out directly to the client, you might consider MVC. MVC makes it much easier to push a file out directly to the client by way of the FilePathResult Class. You can mix and match MVC and WebForms if you like. Scott Hansleman shows how to do this here.

我不确定是直接写回应。在这个特殊的情况下,写是最好的解决方案。我对这一点记忆深刻,但似乎(很久以前)我尝试过通过响应直接写入输出。像你描述的那样,写作和遇到一些奇怪的行为。另一个SO成员应该能够更好地解释为什么会发生这种情况,但是我认为这与您在页面生命周期和IIS流中的位置有关,至于是否响应。写作能达到预期的行为。如果您想将文件直接推送到客户端,可以考虑使用MVC。MVC使得通过FilePathResult类将文件直接推到客户端更加容易。如果您愿意,您可以混合和匹配MVC和WebForms。Scott Hansleman在这里展示如何做到这一点。

If, however, you want to stick with WebForms, then here is a simple page I have thrown together to show how you can use RegisterStartupScript to open either of two local PDFs by clicking the respective button:

但是,如果您想要继续使用WebForms,那么这里有一个简单的页面,我把它放在一起,向您展示如何使用RegisterStartupScript来通过单击相应的按钮来打开本地的两个pdf文件:

ASPX:

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>

Code Behind:

背后的代码:

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