请求带有自定义标头的文件

Request a file with a custom header(请求带有自定义标头的文件)
本文介绍了请求带有自定义标头的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个不寻常的要求.本质上,我需要一种方法,以便当用户单击链接或按钮时,他们将收到 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.

这篇关于请求带有自定义标头的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

jasmine.createSpyObj with properties(jasmine.createSpyObj 与属性)
What#39;s a good way to reuse test code using Jasmine?(使用 Jasmine 重用测试代码的好方法是什么?)
Does Jasmine#39;s toThrow matcher require the argument to be wrapped in an anonymous function?(Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?)
Jasmine - Spying on a method call within a constructor(Jasmine - 在构造函数中监视方法调用)
Getting requirejs to work with Jasmine(让 requirejs 与 Jasmine 一起工作)
How can I spy on a getter property using jasmine?(如何使用 jasmine 监视 getter 属性?)