MiJ*_*Jyn 19 javascript ajax filesize http-headers
基本上,我想弄清楚是否应该使用AJAX下载文件,具体取决于文件大小的大小.
我想这个问题也可以改为:我如何只得到ajax请求的标题?
编辑:ultima-rat0在评论中告诉我两个已经被问过的问题显然和这个问题一样.它们非常相似,但它们都需要jQuery.我想要一个非jQuery解决方案.
Hun*_*oan 31
您可以手动获取XHR响应标头数据:
http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method
此函数将获取所请求URL的文件大小:
function get_filesize(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET",
// to get only the header
xhr.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(parseInt(xhr.getResponseHeader("Content-Length")));
}
};
xhr.send();
}
get_filesize("http://example.com/foo.exe", function(size) {
alert("The size of foo.exe is: " + size + " bytes.");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17322 次 |
| 最近记录: |