javascript 请求带有自定义标头的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10516463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request a file with a custom header
提问by Kris
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 allunless a custom header is sent with it (otherwise it deems the person logged out and sends them to the login screen).
我有一个不寻常的要求。本质上,我需要一种方法,以便当用户单击链接或按钮时,他们将收到 PDF。这里的棘手部分是服务器根本不会处理请求,除非随它发送自定义标头(否则它认为该人已注销并将其发送到登录屏幕)。
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.
- 使用 iframe 或简单地打开一个带有某种路径的新窗口,将返回 PDF。这是行不通的,因为我无法为 PDF 指定所需的标题,并且会在到达 PDF 本身之前被重定向。
- 使用表单并提交请求无法工作,因为我无法向表单添加任何自定义标头(只有 XHR 和插件可以,AFAIK)。
- 使用 XHR 无法工作,因为虽然它可以添加标头并检索文件,但无法将其保存在客户端。
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.
- 使用某种插件(例如 Flash 或 Silverlight)来请求文件。
- 比预期更早地强制更改需求,以便不再需要标题。
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
编辑:这似乎很恰当并证实了我的想法:XMLHttpRequest to open PDF in browser
采纳答案by Esailija
Tested to work in chrome:
经测试可在 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.
您可以将 application/pdf 更改为 application/octet-stream 以获得下载提示。但是从 chrome 的阅读器下载也很容易。
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.
在 Firefox 中什么也没有发生,我想这是因为我没有安装插件来处理application/pdf
。更改为 application/octet-stream 将提示 dl。
With IE I suppose you need some kind of VBScript/ActiveX hackery
使用 IE,我想你需要某种 VBScript/ActiveX 技巧
If the file is huge, using data uri might crash the browser, in that case you can use BlobBuilder and Object URLs.
如果文件很大,使用数据 uri 可能会使浏览器崩溃,在这种情况下,您可以使用 BlobBuilder 和对象 URL。
回答by Farhan Ahmad
Instead of linking to the .PDF file, instead do something like
而不是链接到 .PDF 文件,而是做类似的事情
<a href="pdf_server.php?file=pdffilename">Download my eBook</a>
which outputs a custom header, opens the PDF (binary safe) and prints the data to the user's browser, then they can choose to save the PDF despite their browser settings. The pdf_server.php should look like this:
它输出自定义标题,打开 PDF(二进制安全)并将数据打印到用户的浏览器,然后他们可以选择保存 PDF,而不管他们的浏览器设置。pdf_server.php 应如下所示:
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
EDIT:The only way to add headers to a request from inside a browser (client-side) is use the XmlHttpRequestsetRequestHeader method.
编辑:从浏览器(客户端)内部向请求添加标头的唯一方法是使用XmlHttpRequestsetRequestHeader 方法。
xhr.setRequestHeader('custom-header', 'value');