問題描述
我有一個不尋常的要求.本質上,我需要一種方法,以便當用戶單擊鏈接或按鈕時,他們將收到 PDF.這里棘手的部分是服務器不會處理請求根本,除非隨它一起發送自定義標頭(否則它認為該人已注銷并將其發送到登錄屏幕).p>
目前標題的工作方式無法更改,因此請不要糾纏于此;它將在未來發生變化,并且是我無法控制的內部應用程序.
我探索過的選項:
- 使用 iframe 或簡單地打開一個帶有某種路徑的新窗口,該路徑將返回 PDF.這不起作用,因為我無法為 PDF 指定所需的標頭,并且會在到達 PDF 本身之前被重定向.
- 無法使用表單并提交請求,因為我不能向表單添加任何自定義標題(只有 XHR 和插件可以,AFAIK).
- 使用 XHR 無法正常工作,因為它可以添加標題和檢索文件,無法將其保存在客戶端.
看來我目前唯一的選擇基本上是:
- 使用某種插件(例如 Flash 或 Silverlight)來請求文件.
- 比預期更早地強制要求更改,以便不再需要標頭.
這里有什么我遺漏的嗎?我希望有人可以驗證我的發現或指出我錯過的東西,因為據我所知,我在這里真的無能為力.
這似乎很恰當,并證實了我的想法: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:
- Using an iframe or simply opening a new window with some sort of path that will return the PDF. This can't work because I cannot specify the required header for the PDF and would be redirected before reaching the PDF itself.
- Using a form and submitting the request can't work because I can't add any custom headers to forms (only XHR and plugins can, AFAIK).
- Using XHR can't work because, while it can add the header and retrieve the file, there is no way to save it on the client side.
It would appear my only options at this point are essentially:
- Use some sort of plugin such as Flash or Silverlight to request the file.
- Force the change of the requirement much earlier than expected so that a header is no longer required.
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模板網!