请求一个带有自定义标题的文件 [英] Request a file with a custom header
问题描述
目前标题的工作方式不能改变,所以请不要纠缠于此;它将在未来发生变化,并且是我无法控制的内部应用程序。
我已经探索过的选项:
将任何自定义头文件添加到表单中(只有XHR和插件可以,AFAIK)。
检索文件,没有办法将它保存在客户端。
这是我现在唯一的选择实质上是:
- 使用某种插件(如Flash或Silverlight)来请求文件。 >强制要求的更改比预期更早,以便不再需要标题。
有什么我在这里失踪?我希望有人能够验证我的发现,或者指出我错过了什么,因为据我所知,在这里我没有什么可以做的。
<编辑:这似乎很容易和确认我在想什么: XMLHttpRequest在浏览器中打开PDF
测试在chrome中工作:
函数toBinaryString(data){
var ret = [];
var len = data.length;
var byte; (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); //我的test.pdf在我的本地服务器上有
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);
您可以将application / pdf更改为application / octet-stream以提供下载提示。但是从chrome的阅读器下载也很容易。
在firefox中没有任何反应,我想这是因为我没有插件来处理 application / pdf
已安装。更改为应用程序/八位字节流将提示dl。
使用IE我想你需要某种VBScript / ActiveX hackery
如果文件很大,使用数据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.
这篇关于请求一个带有自定义标题的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!