[英]How to open a pdf file new browser window in javascript
我需要在新的浏览器选项卡中打开pdf文件。 这该怎么做。 我在用
var docLocation = '../downloads/doc.pdf';
window.open(docLocation,"resizeable,scrollbar");
但它会打开浏览器的下载对话框。 怎么做到这一点?
显示pdf的能力完全取决于用户是否有可用于显示pdf的插件,并且还将其设置设置为以这种方式处理pdf文件。
有一些flash小部件可以用来向用户呈现pdf内容,但是直接回答你的问题,你无法控制用户对他们如何选择处理pdf文件的偏好。
此代码将在JavaScript
的完整窗口中打开pdf文档
var pdf = MyPdf.pdf;
window.open(pdf);
打开窗口的功能如下所示:
function openPDF(pdf){
window.open(pdf);
return false;
}
这里
<a href="javascript:void(0);" onclick="javascipt:window.open('YourPDF.pdf');" class="popup">Clic to open.</a>
你需要在你的电脑上安装阅读器
确保Content-Type标头是'application / pdf'而不是'application / octet-stream'
我尝试了所有上述解决方案,但没有一个适用于我,我在mvc 3上运行javascript,并且razor,adobe 11作为插件安装在Chrome和Firefox上。 这是我为了让它在所有上述浏览器上工作而做的。
制作PDF控制器,从这样的javascript调用
在主视图的剃刀代码中:
var URL_OPEN_REPORT_PDF = "@Url.Content("~/Report/OpenPDF/")";
JavaScript的:
var sURL = URL_OPEN_REPORT_PDF;
sURL = AddURLParameter(sURL, "ReportArchive", moControl.treeOrganization.getUserData(sItemUI, "reportarchive"));
window.open(sURL);
controller ReportController.cs:
[Authorize]
[HttpGet]
public ActionResult OpenPDF(string ReportArchive)
{
PDFResult oPdfResult = new PDFResult();
ReportArchive oReportArchive;
var serializer = new JavaScriptSerializer();
oReportArchive = serializer.Deserialize<ReportArchive>(ReportArchive);
string FilePath = Server.MapPath(string.Format("~/Content/Reports/{0}", oReportArchive.FileName));
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
oPdfResult.Length = FileBuffer.LongLength;
oPdfResult.FileBuffer = FileBuffer;
Response.BinaryWrite(FileBuffer);
} return View("PDF", oPdfResult);
}
ViewModel PDFResult.cs:
public class PDFResult
{
/// <summary>
/// Content Length
/// </summary>
public long Length { get; set; }
/// <summary>
/// Content bytes
/// </summary>
public Byte[] FileBuffer { get; set; }
}
查看PDF.cshtml:
@model Report.PDFResult
@{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", Model.Length.ToString());
Layout = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.