今天,上线一个客户网站之后(使用的是广州新一代虚拟空间)发现在读取上传的pdf文件的时候读取错误,通过直接在浏览器输入文件地址的时候发现文件地址被重定向了(呵呵!),结果就是pdf文件源由本地直接变成了跨域获取。解决问题吧!

1、pdf.js获取文件的方法

You can modify the defaultUrl app option in the web/app_options.js file or you can append the ?file= query string to the viewer URL, e.g. http://mozilla.github.com/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf. In the latter case, the PDF path/URL must be encoded using encodeURIComponent().

The viewer can be started without any PDF loaded by setting the defaultUrl app option to an empty string or by using the ?file= query string without any location specified. Use PDFViewerApplication.open(file) to load the PDF file later.

You can use raw binary data to open a PDF document: use Uint8Array instead of URL in the PDFViewerApplication.open call. If you have base64 encoded data, please decode it first -- not all browsers have atob or data URI scheme support. (The base64 conversion operation uses more memory, so we recommend delivering raw PDF data as typed array in first place.)

以上是从其github项目上摘下的大概是有三种方式

  • 设置defaultUrl
  • 通过路径后面带file参数   比如:http://mozilla.github.com/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf
  • 使用PDFViewerApplication.open打开Uint8Array获取的文件流形式

前面两种方法只能获取同源文件,第三种方法就是我需要的跨域获取文件方法了。

2、通过后台(php)获取文件并且将其转换成文件流返回给前端

<?php
//直接使用file_get_contents获取再输出就行
$file = file_get_contents($url);
echo $file;
?>

3、pdf.js通过ajax同步请求获取文件流

var PDFData = "";
var getUrl = "";
var baseUrl = "http://www.zdxhxfzxwx.com.img26752.200cdn.com:9898";
getUrl = baseUrl + getQueryString("filePath");
$.ajax({
type:"post",
async:false, //
contentType: "application/x-www-form-urlencoded",
mimeType: 'text/plain; charset=x-user-defined',
url:"/plus/getFileToBinary.php",
success:function(data){
PDFData = data;
},
data: {
"url": getUrl,
}
});
var rawLength = PDFData.length;
console.log(rawLength);
//转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = PDFData.charCodeAt(i) & 0xff;
}
DEFAULT_URL = array;
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}

这个方法有问题:1、php服务器要支持文件读取(可以通过修改php配置文件前提是有这个修改权限)

        2、php读取并输出文件执行时间比较长

pdf.js如何跨域读取pdf文件?的更多相关文章

  1. 使用pdf.js预览实现读取服务器外部文件

    不知道大家使用百度网盘的文件预览功能,f12看过控制台没有. 发现百度网盘使用的预览文件功能全是基于开源pdf .js的 接下来正题,我们在使用pdf.js默认是读取发布容器内部的文件,读取外部的文件 ...

  2. 利用PDF.JS插件解决了本地pdf文件在线浏览问题(根据需要隐藏下载功能,只保留打印功能)

    我是在IE11和谷歌上做的测试,都可以显示,把做出的东西记录下来,方便大家还有自己学习! 可以在IIS7服务器上也可以下载Tomcat来做服务器 Tomcat下载地址   http://pan.bai ...

  3. vue -- config.js 配置跨域文件

    1.在使用vue开发的时候经常要涉及到跨域的问题,其实在vue cli中是有我们设置跨域请求的文件的. 2.当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev -- ...

  4. JSON跨域读取那点事(JSONP跨域访问)

    最近在码一个小项目,需要远程读取json.因为需求很少,如果引用jquery使用其getjson方法就显得很浪费嘛= = 这篇文章很详细的解释了JSON跨域读取的前世今生,把原理讲得很透彻.特此分享. ...

  5. js调用跨域

    web aapi 初体验 解决js调用跨域问题   跨域界定 常见跨域: 同IP不同端口: http:IP:8001/api/user     http:IP:8002/api/user 不同IP不同 ...

  6. js&jquery跨域详解jsonp,jquery并发大量请求丢失回调bug

    URL  说明 是否允许通信 http://www.a.com/a.js http://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.js http:/ ...

  7. 关于跨域读取json数据我的理解

    这两天在做关于读取json数据的插件,想用getJSON读取数据: $.getJSON(http://www.xxxx.com/Titles.js, function (data) { console ...

  8. JS JSOP跨域请求实例详解

    JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.这篇文章主要介绍了JS JSOP跨域请求实例详解的相关资料,需要的朋友可以参考下 ...

  9. springcloud 微服务 分布式 Activiti6 工作流 vue.js html 跨域 前后分离

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

随机推荐

  1. static作用域

    当一个函数完成时,它的所有变量通常都会被删除.然而,有时候您希望某个局部变量不要被删除. 要做到这一点,请在您第一次声明变量时使用 static 关键字: <?php function myTe ...

  2. Gson杂记录

    //Integer userId = getUserId(); //System.out.println("userId:"+userId); /*for(int i=0;i< ...

  3. Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route

    题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...

  4. 笔记-python-语法-property

    笔记-python-语法-property 1.      property 看到@property,不明白什么意思,查找文档了解一下. 1.1.    property类 proerty是pytho ...

  5. 3687: 简单题(bitset)

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 700  Solved: 319[Submit][Status][Discuss] ...

  6. 一些 ssh 小技巧

    本文来自网易云社区. 作者:沈高峰 ssh 经常需要使用的,每次使用都  ssh  abc@XXX.def.com -p 12138 -i ~/.ssh/id_rsa 来一遍显然太麻烦了,下面分享一点 ...

  7. 浅析src与href的区别

    src与href的区别 SRC src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请 ...

  8. 2013 ACM/ICPC Asia Regional Changsha Online – C题 Color Representation Conversion (坑爹模拟题)

    题意:给你三种颜色表示模式,RGB,HSV和HSL,实现任意模式之间两两转化. 1.最好别看题目中给的转化公式描述,我觉得叙述的一点也不清楚,看维基百科,把维基百科上的公式一句一句翻译过来就好 2.在 ...

  9. HTML5初识Canvas

    HTML5初识Canvas <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  10. Java String.intern()_学习笔记

    参考:https://www.jianshu.com/p/0d1c003d2ff5 String.intern() String.intern()是native方法,底层调用c++中的StringTa ...