久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

請求帶有自定義標頭的文件

Request a file with a custom header(請求帶有自定義標頭的文件)
本文介紹了請求帶有自定義標頭的文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個不尋常的要求.本質上,我需要一種方法,以便當用戶單擊鏈接或按鈕時,他們將收到 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 国产成人综合在线 | 午夜影院污 | 中文字幕免费观看 | 亚洲欧洲成人av每日更新 | 国产福利在线播放麻豆 | 午夜免费福利影院 | 中文字幕国产 | 毛片高清| h视频免费观看 | 亚卅毛片 | 亚洲精品一区二区二区 | 日韩在线视频观看 | h片在线看 | 91国在线高清视频 | 天天射影院 | 黄视频免费在线 | 国产精品久久久久久久久免费樱桃 | 天天成人综合网 | 毛片网在线观看 | 亚洲成人免费视频在线 | 国产精品一区二区三区四区 | 日韩a在线 | 欧美国产日韩在线 | 亚洲三区在线观看 | 伊人免费在线观看高清 | 日本免费视频在线观看 | 欧美一区二区 | 一区二区三区四区在线 | 精品乱码一区二区 | 97免费视频在线观看 | 欧美一级大片 | 美女在线观看国产 | 中文二区 | 国产一级一级 | 看真人视频一级毛片 | 久久久久久黄 | 国产免费一区二区 | 97久久精品 | 99精品免费久久久久久久久日本 | 国产清纯白嫩初高生视频在线观看 | 韩日一区二区 |