C# 如何在新选项卡或窗口中打开PDF文件而不是下载它(使用asp.net)?

C# 如何在新选项卡或窗口中打开PDF文件而不是下载它(使用asp.net)?,c#,asp.net,pdf,stream,C#,Asp.net,Pdf,Stream,这是下载文件的代码 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-dispo

这是下载文件的代码

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();
当执行此代码时,它将要求用户打开或保存文件。相反,我需要打开一个新的选项卡或窗口并显示文件。我怎样才能做到这一点

注意:

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

这可能会有所帮助

Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
Response.Write(“”);
Response.Write(“window.open('../Inventory/pages/printableads.pdf',''u newtab');”);
回答。写(“”);

与其将流加载到字节数组中并将其写入响应流,不如看看

如果希望在新窗口中打开PDF,则必须在新窗口中打开下载页面,例如:

<a href="viewpdf.aspx" target="_blank">View PDF</a>

使用此代码。这就像一个冠军

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();

您必须使用代码创建另一个页面或通用处理程序来生成pdf。然后该事件被触发,此人被重定向到该页面

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


在javaScript中:

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

函数openInNewTab(){
window.document.forms[0]。目标='_blank';
setTimeout(函数(){window.document.forms[0]。目标=“”;},0);
}

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

您可以从MVC操作返回文件结果。

*********************MVC行动************

    public FileResult OpenPDF(parameters)
    {
       //code to fetch your pdf byte array
       return File(pdfBytes, "application/pdf");
    }
**************js**************

    public FileResult OpenPDF(parameters)
    {
       //code to fetch your pdf byte array
       return File(pdfBytes, "application/pdf");
    }
使用formpost将您的数据发布到行动中

    var inputTag = '<input name="paramName" type="text" value="' + payloadString + '">';
    var form = document.createElement("form");
    jQuery(form).attr("id", "pdf-form").attr("name", "pdf-form").attr("class", "pdf-form").attr("target", "_blank");
    jQuery(form).attr("action", "/Controller/OpenPDF").attr("method", "post").attr("enctype", "multipart/form-data");
    jQuery(form).append(inputTag);
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
    return false;
var inputTag='';
var form=document.createElement(“表单”);
jQuery(form).attr(“id”,“pdf form”).attr(“name”,“pdf form”).attr(“class”,“pdf form”).attr(“target”,“_blank”);
jQuery(form).attr(“action”,“Controller/OpenPDF”).attr(“method”,“post”).attr(“enctype”,“multipart/formdata”);
jQuery(form).append(inputTag);
文件.正文.附件(表格);
表单提交();
文件.body.removeChild(表格);
返回false;
您需要创建一个表单来发布数据,将其附加到dom中,发布数据并删除文档体中的表单


但是,form post不会仅在边缘浏览器上将数据发布到新选项卡。但是,获取请求的工作原理是,它只是打开一个新选项卡,其中包含一个url,其中包含用于操作参数的查询字符串。

这里我使用iTextSharp dll生成PDF文件。 我想打开PDF文件而不是下载它。 所以我使用下面的代码,这对我来说很好。 现在pdf文件正在浏览器中打开,现在正在加载

        Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        Paragraph Text = new Paragraph("Hi , This is Test Content");
        pdfDoc.Add(Text);
        pdfWriter.CloseStream = false;
        pdfDoc.Close();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.End();

如果你想下载文件, 在此响应后添加以下行。ContentType=“application/pdf”


我将如何将下载文件称为此??这是我的问题。我应该创建它以供阅读,以后也可以保存它。问题是我不能提供文件的原始名称或文件的路径。所以我必须完成上面的步骤下载它,然后用另一个名字打开它。你能在服务器上做一个临时副本吗?你可以尝试
内容处理:内联
而不是
附件
-这篇文章可能会帮助你了解更多我感兴趣的第一步。第二步在我的程序中不实用。bcz i无法将原始路径发送到它。使用第一步,我可以将文件重定向到另一个窗口吗?您不能从服务器端打开新窗口。您必须在新窗口中打开下载页面,并在该窗口中使用TransmitFile将文件返回给用户。您的链接是什么样子的?为什么你不能给它添加一个目标?如果出于某种原因无法更改a标记的源代码(例如,它是为您生成的),您可以使用JavaScript添加它,您可以在服务器端这样做。Hyperlink Hyperlink=新建超链接();hyper.NavigateUrl=“PDFViewer.aspx”;添加(“目标”,“空白”);不幸的是,只有当您在某处存储了一个实际文件时,这种方法才有效。例如,如果您有一个已生成的字节数组,则没有“路径”-rendering Response.TransmitFile无用。至少我遇到了这种情况。我尝试了这种方法,效果非常好,谢谢:)你的C代码在服务器上运行。因此,Process.Start在服务器上启动进程,而不是在计算机上启动进程。您根本不可能直接在客户机上启动流程。谢谢,这对我很有用。
        Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        Paragraph Text = new Paragraph("Hi , This is Test Content");
        pdfDoc.Add(Text);
        pdfWriter.CloseStream = false;
        pdfDoc.Close();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.End();
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");