![](/img/trans.png)
[英]How to open PDF file in a new tab or window instead of downloading it using C# and ASP.NET MVC?
[英]How to open PDF file in a new tab or window instead of downloading it (using 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();
执行此代码后,它将要求用户打开或保存文件。 取而代之的是,我需要打开一个新的选项卡或窗口并显示文件。 我该如何实现?
注意:
文件不必位于网站文件夹中。 它可能位于另一个文件夹中。
与其将流加载到字节数组中并将其写入响应流,还不如看看HttpResponse.TransmitFile。
Response.ContentType = "Application/pdf";
Response.TransmitFile(pathtofile);
如果要在新窗口中打开PDF,则必须在新窗口中打开下载页面,例如:
<a href="viewpdf.aspx" target="_blank">View PDF</a>
Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);
和
<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>
在javaScript中:
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
请务必重置target ,否则所有其他调用(如Response.Redirect
将在新选项卡中打开,这可能不是您想要的。
这可能会有所帮助
Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
您必须使用代码创建另一个页面或通用处理程序,以生成pdf。 然后触发该事件,并将该人重定向到该页面。
您可以从MVC操作返回FileResult。
********************* MVC动作************
public FileResult OpenPDF(parameters)
{
//code to fetch your pdf byte array
return File(pdfBytes, "application/pdf");
}
************** js **************
使用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;
您需要创建一个表单来发布您的数据,将其附加到您的dom,发布您的数据并删除该表单您的文档正文。
但是,表单发布不会仅在EDGE浏览器上将数据发布到新选项卡。 但是, 获取请求可以正常工作,因为它只是打开一个新标签,并在其中包含包含您的操作参数查询字符串的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();
如果要下载文件,请在此Response.ContentType =“ application / pdf”之后添加以下行:
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
使用此代码。 这就像冠军。
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.