加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在新的选项卡或窗口中打开PDF文件,而不是下载它(使用a

发布时间:2020-12-15 06:48:43 所属栏目:百科 来源:网络整理
导读:这是下载文件的代码. 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,(int)fs.Length);fs.Close();Response.AddHeader("con
这是下载文件的代码.
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,(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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读