This is the code for downloading the file.
这是下载文件的代码。
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?
执行此代码时,它将要求用户打开或保存文件。而不是这个我需要打开一个新的选项卡或窗口并显示该文件。我怎样才能做到这一点?
NOTE:
注意:
File won't necessary be located in the website folder. It might be located in an other folder.
文件不必位于网站文件夹中。它可能位于其他文件夹中。
7 个解决方案
#1
15
Instead of loading a stream into a byte array and writing it to the response stream, you should have a look at HttpResponse.TransmitFile
您应该查看HttpResponse.TransmitFile,而不是将流加载到字节数组并将其写入响应流。
Response.ContentType = "Application/pdf";
Response.TransmitFile(pathtofile);
If you want the PDF to open in a new window you would have to open the downloading page in a new window, for example like this:
如果您希望在新窗口中打开PDF,则必须在新窗口中打开下载页面,例如:
<a href="viewpdf.aspx" target="_blank">View PDF</a>
#2
5
Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);
And
和
<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>
In javaScript:
在javaScript中:
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
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.
注意重置目标,否则所有其他调用如Response.Redirect将在新选项卡中打开,这可能不是您想要的。
#3
3
this may help
这可能有所帮助
Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
#4
1
You have to create either another page or generic handler with the code to generate your pdf. Then that event gets triggered and the person is redirected to that page.
您必须使用代码创建另一个页面或通用处理程序以生成您的pdf。然后该事件被触发,该人被重定向到该页面。
#5
0
you can return a FileResult from your MVC action.
您可以从MVC操作返回FileResult。
*********************MVC action************
********************* MVC行动************
public FileResult OpenPDF(parameters)
{
//code to fetch your pdf byte array
return File(pdfBytes, "application/pdf");
}
**************js**************
************** JS **************
Use formpost to post your data to action
使用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;
You need to create a form to post your data, append it your dom, post your data and remove the form your document body.
您需要创建一个表单来发布您的数据,将其附加到您的dom,发布您的数据并删除您的文档正文。
However, form post wouldn't post data to new tab only on EDGE browser. But a get request works as it's just opening new tab with a url containing query string for your action parameters.
但是,表单帖子不会仅在EDGE浏览器上将数据发布到新选项卡。但是get请求有效,因为它只是打开一个新的选项卡,其中包含一个包含操作参数查询字符串的url。
#6
0
Here I am using iTextSharp dll for generating PDF file. I want to open PDF file instead of downloading it. So I am using below code which is working fine for me. Now pdf file is opening in browser ,now dowloading
这里我使用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();
If you want to download file, add below line, after this Response.ContentType = "application/pdf";
如果要下载文件,请在此Response.ContentType =“application / pdf”之后添加以下行;
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
#7
-4
Use this code. This works like a champ.
使用此代码。这就像一个冠军。
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();
#1
15
Instead of loading a stream into a byte array and writing it to the response stream, you should have a look at HttpResponse.TransmitFile
您应该查看HttpResponse.TransmitFile,而不是将流加载到字节数组并将其写入响应流。
Response.ContentType = "Application/pdf";
Response.TransmitFile(pathtofile);
If you want the PDF to open in a new window you would have to open the downloading page in a new window, for example like this:
如果您希望在新窗口中打开PDF,则必须在新窗口中打开下载页面,例如:
<a href="viewpdf.aspx" target="_blank">View PDF</a>
#2
5
Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);
And
和
<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>
In javaScript:
在javaScript中:
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
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.
注意重置目标,否则所有其他调用如Response.Redirect将在新选项卡中打开,这可能不是您想要的。
#3
3
this may help
这可能有所帮助
Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
#4
1
You have to create either another page or generic handler with the code to generate your pdf. Then that event gets triggered and the person is redirected to that page.
您必须使用代码创建另一个页面或通用处理程序以生成您的pdf。然后该事件被触发,该人被重定向到该页面。
#5
0
you can return a FileResult from your MVC action.
您可以从MVC操作返回FileResult。
*********************MVC action************
********************* MVC行动************
public FileResult OpenPDF(parameters)
{
//code to fetch your pdf byte array
return File(pdfBytes, "application/pdf");
}
**************js**************
************** JS **************
Use formpost to post your data to action
使用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;
You need to create a form to post your data, append it your dom, post your data and remove the form your document body.
您需要创建一个表单来发布您的数据,将其附加到您的dom,发布您的数据并删除您的文档正文。
However, form post wouldn't post data to new tab only on EDGE browser. But a get request works as it's just opening new tab with a url containing query string for your action parameters.
但是,表单帖子不会仅在EDGE浏览器上将数据发布到新选项卡。但是get请求有效,因为它只是打开一个新的选项卡,其中包含一个包含操作参数查询字符串的url。
#6
0
Here I am using iTextSharp dll for generating PDF file. I want to open PDF file instead of downloading it. So I am using below code which is working fine for me. Now pdf file is opening in browser ,now dowloading
这里我使用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();
If you want to download file, add below line, after this Response.ContentType = "application/pdf";
如果要下载文件,请在此Response.ContentType =“application / pdf”之后添加以下行;
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
#7
-4
Use this code. This works like a champ.
使用此代码。这就像一个冠军。
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();