我有一个不寻常的要求.本质上,我需要一种方法,以便当用户单击链接或按钮时,他们将收到 PDF.这里棘手的部分是服务器不会处理请求根本,除非随它一起发送自定义标头(否则它认为该人已注销并将其发送到登录屏幕).p>
目前标题的工作方式无法更改,因此请不要纠缠于此;它将在未来发生变化,并且是我无法控制的内部应用程序.
我探索过的选项:
看来我目前唯一的选择基本上是:
这里有什么我遗漏的吗?我希望有人可以验证我的发现或指出我错过的东西,因为据我所知,我在这里真的无能为力.
这似乎很恰当,并证实了我的想法:XMLHttpRequest to open PDF in浏览器
经过测试可以在 chrome 中工作:
function toBinaryString(data) {var ret = [];var len = 数据长度;变量字节;for (var i = 0; i < len; i++) {byte=(data.charCodeAt(i)&0xFF)>>>0;ret.push(String.fromCharCode(byte));}返回 ret.join('');}var xhr = 新的 XMLHttpRequest;xhr.open("GET", "/test.pdf");//我在本地服务器上有 test.pdfxhr.addEventListener(加载",函数(){var data = toBinaryString(this.responseText);数据=数据:应用程序/pdf;base64,"+btoa(数据);文档位置=数据;}, 错误的);xhr.setRequestHeader("magic", "header" );xhr.overrideMimeType("application/octet-stream; charset=x-user-defined;" );xhr.send(null);
您可以将 application/pdf 更改为 application/octet-stream 以获得下载提示.但也很容易从 chrome 阅读器下载.
在 Firefox 中没有任何反应,我猜这是因为我没有安装插件来处理 application/pdf
.更改为 application/octet-stream 将提示 dl.
对于 IE,我想你需要某种 VBScript/ActiveX 黑客技术
如果文件很大,使用 data uri 可能会导致浏览器崩溃,在这种情况下,您可以使用 BlobBuilder 和 Object URL.
I have an unusual requirement. Essentially I need a way so that, when the user clicks on a link or button, they will receive a PDF. The tricky part here is that the server won't process the request at all unless a custom header is sent with it (otherwise it deems the person logged out and sends them to the login screen).
At the moment the way the header works cannot be changed so please don't dwell on it; it will get changed in the future and is an internal application that I have no control over.
The options I have explored:
It would appear my only options at this point are essentially:
Is there anything I am missing here? I'm hoping someone can either verify my findings or point me to something I missed because, as far as I can tell, there isn't really anything I can do here.
EDIT: This seems apt and confirms what I was thinking: XMLHttpRequest to open PDF in browser
Tested to work in chrome:
function toBinaryString(data) {
var ret = [];
var len = data.length;
var byte;
for (var i = 0; i < len; i++) {
byte=( data.charCodeAt(i) & 0xFF )>>> 0;
ret.push( String.fromCharCode(byte) );
}
return ret.join('');
}
var xhr = new XMLHttpRequest;
xhr.open( "GET", "/test.pdf" ); //I had test.pdf this on my local server
xhr.addEventListener( "load", function(){
var data = toBinaryString(this.responseText);
data = "data:application/pdf;base64,"+btoa(data);
document.location = data;
}, false);
xhr.setRequestHeader("magic", "header" );
xhr.overrideMimeType( "application/octet-stream; charset=x-user-defined;" );
xhr.send(null);
You can change application/pdf to application/octet-stream to have download prompt. But it's pretty easy to download from the chrome's reader as well.
In firefox nothing happens I guess it's because I don't have a plugin to deal with application/pdf
installed. Changing to application/octet-stream will prompt a dl.
With IE I suppose you need some kind of VBScript/ActiveX hackery
If the file is huge, using data uri might crash the browser, in that case you can use BlobBuilder and Object URLs.
这篇关于请求带有自定义标头的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!