您的位置:首页 > 移动开发 > Objective-C

PDF在线预览 ,兼容IE,chrome。使用了pdfobject.js

2014-12-03 14:51 996 查看
PDF在线预览,兼容IE,CHROME

http://files.cnblogs.com/w519/PDFViewSolution.rar

本人写的PDF在线预览DEMO使用了pdfobject.js和adobe Reader阅读器。

因为PDFobjcet.js不兼容IE浏览器,而,所以在chrome浏览器上pdfobjcet.js打开PDF,而IE浏览器中使用adobe Reader阅读器打开PDF。

问题:对于浏览器中设置了默认迅雷下载,当单击网页中a标签链接时候,迅雷监测“链接”,带有.pdf,.exe等结尾扩展名,默认为下载,不能跳转页面。

方法:可以将url中文件扩展名去掉,就不会迅雷下载!

1.在Default.aspx页面判断浏览器,如果是IE,且判断是否安装了adobe Reader,跳转到PDFView.aspx页面,在后台输出PDF文件。如果是chrome就用pdfobject.js,在本页面输出PDF

Default.aspx代码

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/pdfobject.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var w = $(document).width();
var h = $(document).height();
$("#pdf1").css("width", w).css("height", h);

// 下面代码都是处理IE浏览器的情况
if (window.ActiveXObject || "ActiveXObject" in window) {
//判断是否为IE浏览器,"ActiveXObject" in window判断是否为IE11
//判断是否安装了adobe Reader
for (x = 2; x < 10; x++) {
try {
oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
if (oAcro) {
flag = true;
}
} catch (e) {
flag = false;
}
}
try {
oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
if (oAcro4) {
flag = true;

}
} catch (e) {
flag = false;
}
try {
oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
if (oAcro7) {
flag = true;
}
} catch (e) {
flag = false;
}
if (flag) {
$('#pdf1').hide();
location = "PDFView.aspx";
}
else {
alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装!");
location = "http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe";
}
}
else {
//alert($.trim(urls));
var success = new PDFObject({ url: 'PDF/19-P1012728-064_ZHCN-Draft.pdf', pdfOpenParams: { scrollbars: '0', toolbar: '0', statusbar: '0'} }).embed("pdf1");
if (!success) {
var opts = {
width:$(document).width(),
height: $(document).height(),
autoplay: true
};
}
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="pdf1" >对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装!
<a href="PDF/19-P1012728-064_ZHCN-Draft.pdf">PDF/19-P1012728-064_ZHCN-Draft.pdf</a>
</div>
</form>


View Code
PDFView.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Browser.Browser.ToLower() != "chrome")
{

string filePath = Server.MapPath("pdf/19-P1012728-064_ZHCN-Draft.pdf");
Response.ClearContent();
Response.ClearHeaders();
string FilePost = filePath.Substring(filePath.Length - 3).ToLower();
switch (FilePost)
{
case "pdf":
Response.ContentType = "application/PDF";
break;
case "doc":
Response.ContentType = "application/msword";
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
break;
default:
Session["ErrorInfo"] = "不支持的文件格式:" + FilePost;
Response.Redirect("ErrorPage.aspx");
break;
}
Response.WriteFile(filePath);
Response.Flush();
Response.Close();
Session.Remove("Report");

}
else if (Request.Browser.Browser.ToLower() == "chrome")
{

string filePath = Server.MapPath("pdf/19-P1012728-064_ZHCN-Draft.pdf");
Response.ClearContent();
Response.ClearHeaders();
string FilePost = filePath.Substring(filePath.Length - 3).ToLower();
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;

if (Request.Browser.Browser == "Firefox")
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "1.pdf");
else
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("1.pdf", System.Text.Encoding.UTF8));

using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open))
{
byte[] by = new byte[fs.Length];
fs.Read(by, 0, by.Length);
Response.BinaryWrite(by);
Response.AddHeader("Accept-Language", "zh-tw");
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Length ", by.Length.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: