How to open a pdf file new browser window in javascript
I need to open a pdf file in new browser tab. How to d开发者_如何学Co this. I was using
var docLocation = '../downloads/doc.pdf';
window.open(docLocation,"resizeable,scrollbar");
But it opens a download dialog box of the browser. How to achieve this ?
This code will open a pdf document in a full window from JavaScript
var pdf = MyPdf.pdf;
window.open(pdf);
A function to open windows would look like this:
function openPDF(pdf){
window.open(pdf);
return false;
}
The ability to display the pdf is entirely dependent on whether the user has a plugin available to display the pdf and also has their settings set to treat pdf files this way.
There are some flash widgets out there that can be used to present pdf content to the user but to directly answer your question, you cannot control the users preferences for how they chose to handle pdf files.
here
<a href="javascript:void(0);" onclick="javascipt:window.open('YourPDF.pdf');" class="popup">Clic to open.</a>
you need to have installed reader in your pc
Make sure the Content-Type header is 'application/pdf' and not 'application/octet-stream'
I tried all of the above solutions and none of them worked for me, I'm running javascript on top of mvc 3, and razor, adobe 11 installed as add-on on ie, Chrome and Firefox. Here is what I did in order to get it to work on all above browsers.
Made PDF controller, called from javascript like this
in razor code for main view:
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; }
}
View PDF.cshtml:
@model Report.PDFResult
@{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", Model.Length.ToString());
Layout = null;
}
精彩评论