![](/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.