vb.net 使用 ASP.NET 二进制写入新选项卡/窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24535910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:48:29  来源:igfitidea点击:

Binary Write using ASP.NET into a new tab / window

asp.netvb.netvisual-studio-2012responsecontenttype

提问by Hyman

I am using VB.NET to write a pdf file directly to the browser, which opens it right away replacing the content in a current window. I am trying to do is set a parameter which will cause binarywrite in a new tab / window.

我正在使用 VB.NET 将 pdf 文件直接写入浏览器,浏览器会立即打开它以替换当前窗口中的内容。我想做的是设置一个参数,该参数将导致在新选项卡/窗口中进行二进制写入。

' Set the appropriate ContentType.
Response.ContentType = "Application/pdf"

' Write file directly to browser.
Response.BinaryWrite(binaryData)
Response.End()

There is has to be something that I can set which will cause this to write binary PDF in a new window. Like Response.Target = "_blank" ?????

必须有一些我可以设置的东西,这将导致它在新窗口中写入二进制 PDF。像 Response.Target = "_blank" ?????

回答by Hyman

First, create an additional page named "PDF.aspx" or whatever you would like it called.

首先,创建一个名为“PDF.aspx”的附加页面或您希望它调用的任何名称。

Second, in your page, store your "binarydata" variable in a session.

其次,在您的页面中,将您的“binarydata”变量存储在会话中。

Session("binaryData") = binarydata

And tell it to go to your new page.

并告诉它转到您的新页面。

Response.Redirect("PDF.aspx")

Third, go to your PDF.aspx code behind page and put the following:

第三,转到您的 PDF.aspx 代码隐藏页面并输入以下内容:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles       Me.Load
    Try
         Response.ContentType = "Application/pdf"
         Response.BinaryWrite(Session("binaryData"))
         Response.End()
    Catch ex As Exception
        Throw ex
    End Try
End Sub

That should produce the result you want. :)

那应该会产生你想要的结果。:)

回答by mason

How about an alternative? Write to the browser some client side script to open a new window/tab.

替代方案怎么样?向浏览器写入一些客户端脚本以打开一个新窗口/选项卡。

ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "window.open('MyPDFDisplayer.ashx');");

Then create a new generic handlerMyPDFDisplayer.ashx that writes the PDF to the window.

然后创建一个新的通用处理程序MyPDFDisplayer.ashx 将 PDF 写入窗口。

context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(binaryData);
context.Response.End();

I'm a C# guy, so you'll have to translate but it should be pretty easy.

我是一个 C# 人,所以你必须翻译,但它应该很容易。

回答by Bonez024

I know this is old but here's what I had done to achieve the OP was after. (Forgive me as this is C# but it shouldn't be too hard to convert it)

我知道这已经过时了,但这是我为实现 OP 所做的工作。(请原谅我,因为这是 C#,但转换它应该不会太难)

This is the original code that I had used that displayed the same behavior that you have described!

这是我使用的原始代码,它显示了您所描述的相同行为!

Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + docName);
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.BinaryWrite(byteArray);
Response.End();

There are two key changes that I had to make to get this to work the way I wanted (Download the file as PDF, then open in a new tab when the user had clicked on that attachment.)and they are both on line 3 of the code above.

我必须进行两个关键更改才能使其按我想要的方式工作(将文件下载为 PDF,然后在用户单击该附件时在新选项卡中打开。)它们都位于第 3 行上面的代码。

Change this

改变这个

Response.AddHeader("Content-Disposition", "inline; filename=" + docName);

To this

对此

Response.AddHeader("Content-Disposition", "attachment; filename=" + docName + ".pdf");

If you specify the Content-Disposition as "Inline"it will output the file content in your current window.

如果您将 Content-Disposition 指定为“内联”,它将在当前窗口中输出文件内容。

You need to change that disposition to "attachment"AND make sure your extension suffix is explicitly set to .pdf.

您需要将该配置更改为“附件”,并确保您的扩展后缀明确设置为 .pdf。