请求一个带有自定义标题的文件 [英] Request a file with a custom header

查看:149
本文介绍了请求一个带有自定义标题的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的要求。基本上我需要一种方式,当用户点击链接或按钮时,他们会收到PDF。这里棘手的部分是服务器不会处理请求 ,除非自定义头部与它一起发送(否则它认为该人员已注销并将其发送到登录屏幕)。 p>

目前标题的工作方式不能改变,所以请不要纠缠于此;它将在未来发生变化,并且是我无法控制的内部应用程序。



我已经探索过的选项:


  • 使用iframe或者简单地打开一个具有返回PDF的某种路径的新窗口。这是行不通的,因为我无法指定PDF所需的标题,并且在到达PDF本身之前会被重定向。 使用表单并提交请求无法工作,因为我可以't
    将任何自定义头文件添加到表单中(只有XHR和插件可以,AFAIK)。
  • 使用XHR无法正常工作,因为它可以添加头文件和
    检索文件,没有办法将它保存在客户端。



这是我现在唯一的选择实质上是:


  • 使用某种插件(如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆