我想通过浏览器开始简单的文件下载,但是必须使用自定义HTTP标头传递访问令牌:
GET https://my.site.com/some/file
Authorization: access_token
Run Code Online (Sandbox Code Playgroud)
如何在网站URL后面注入Authorization:标头?我知道使用查询字符串可以做到这一点,但我想用标题来做.
我熟悉XMLHttpRequest,但据我所知,它不会触发下载,它只读取内容,我想下载的文件至少是几百MB.
xhr.setRequestHeader('Authorization', 'access_token');
Run Code Online (Sandbox Code Playgroud)
这看起来像一个简单的任务,但我是没有经验的编码器,所以任何帮助都会很好.谢谢.
我正在对一个返回二进制数据的API进行ajax调用.我想知道是否可以获取二进制数据并在新窗口中为客户端显示它?这就是我现在正在做的事情.问题是,文档打开了,但它完全是空白的.
$.ajax({
type: "POST",
url: apiURL,
data: xmlRequest,
complete: function(xhr, status) {
var bb = new window.WebKitBlobBuilder();
// Append the binary data to the blob
bb.append(xhr.responseText);
var blobURL = window.webkitURL.createObjectURL(bb.getBlob('application/pdf'));
window.open(blobURL);
}
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试创建一个google-chrome-extension,它将下载一个mp3文件.我正在尝试使用HTML5 blob和iframe来触发下载,但它似乎不起作用.这是我的代码:
var finalURL = "server1.example.com/u25561664/audio/120774.mp3";
var xhr = new XMLHttpRequest();
xhr.open("GET", finalURL, true);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
var bb = new (window.BlobBuilder || window.WebKitBlobBuilder)();
bb.append(xhr.responseText);
var blob = bb.getBlob("application/octet-stream");
var saveas = document.createElement("iframe");
saveas.style.display = "none";
saveas.src = window.webkitURL.createObjectURL(blob);
document.body.appendChild(saveas);
delete xhr;
delete blob;
delete bb;
}
}
xhr.send();
Run Code Online (Sandbox Code Playgroud)
在控制台中查看时,正确创建了blob,设置看起来正确:
大小:15312172类型:"application/octet-stream"
但是,当我尝试创建的链接时createObjectURL(),
BLOB:铬扩展:// dkhkkcnjlmfnnmaobedahgcljonancbe/b6c2e829-c811-4239-bd06-8506a67cab04
我收到一份空白文件和警告说
资源解释为Document但使用MIME类型application/octet-stream传输.
如何让我的代码正确下载文件?
我想做XMLHttpRequest然后通过POST方法发送文件名在浏览器中打开PDF.
xmlhttp.open("POST","pdf.php",true); //CHANGE
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("file="+input);
Run Code Online (Sandbox Code Playgroud)
这是可能的还是XMLHttpRequest只适用于HTML?
javascript ×3
html5 ×2
ajax ×1
binary-data ×1
blob ×1
download ×1
html ×1
http-headers ×1
jquery ×1
pdf ×1
php ×1