如何在新选项卡或窗口中打开 PDF 文件而不是下载它(使用 asp.net)? [英] How to open PDF file in a new tab or window instead of downloading it (using asp.net)?

查看:37
本文介绍了如何在新选项卡或窗口中打开 PDF 文件而不是下载它(使用 asp.net)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是下载文件的代码.

System.IO.FileStream fs = new System.IO.FileStream(Path+"\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();

当这段代码被执行时,它会要求用户打开或保存文件.而不是这个,我需要打开一个新选项卡或窗口并显示该文件.我怎样才能做到这一点?

When this code is executed, it will ask user to open or save the file. Instead of this I need to open a new tab or window and display the file. How can I achieve this?

注意:

文件不必位于网站文件夹中.它可能位于其他文件夹中.

File won't necessary be located in the website folder. It might be located in an other folder.

推荐答案

Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);

<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>

在 JavaScript 中:

In javaScript:

<script type="text/javascript">
    function openInNewTab() {
        window.document.forms[0].target = '_blank'; 
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
    }
</script>

注意重置目标,否则所有其他调用(如Response.Redirect)将在新选项卡中打开,这可能不是您想要的.

Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.

这篇关于如何在新选项卡或窗口中打开 PDF 文件而不是下载它(使用 asp.net)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆