【发布时间】:2013-05-16 23:10:33
【问题描述】:
我正在开发一个 JScript/VBScript HTML 应用程序(因此我们可以以比普通网页更高的权限执行),我们正在提供下载文件的功能,并且需要向我们的用户提供有关下载进度的反馈。自然地,我根据总文件大小和已下载文件的数量计算了一个进度条或进度文本。我正在尝试在 Internet Explorer 中获取已下载部分的大小。到目前为止,我已经尝试了所有我能想到的方法来访问已经下载了多少文件。在readyState 3(交互式)期间尝试访问responseText或responseBody时,我只是得到了异常:
完成此操作所需的数据尚不可用。
因此,当他们记录下 responseText 或 responseBody 在 XMLHttpRequest 完成之前将不可用(进入 readyState 4)时,微软似乎没有撒谎
我可以获得总文件大小并将文件保存到磁盘,因此我省略了我已经拥有的代码部分。这是我一直在尝试的。我愿意接受任何其他建议——甚至改变 XMLHttpRequest 的实现。请注意,我无法控制服务器端...只有我的客户端应用程序,因此 php/ASP 解决方案将不起作用。 提前谢谢...
function getRemoteFile(urlToFile){
if (urlToFile) {
try {
var xmlReq = new XMLHttpRequest();
//Tried all of the following with no luck
//var xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
//var xmlReq = new ActiveXObject('Msxml2.DOMDocument.3.0');
//var xmlReq = new ActiveXObject("Msxml2.XMLHTTP.3.0");
//var xmlReq = new ActiveXObject("Msxml2.XMLHTTP.6.0");
//var xmlReq =new ActiveXObject("Msxml2.DOMDocument.6.0");
function updateDownloadProgress() {
if (xmlReq.readyState == 3) {
try {
alert(xmlReq.responseText.length);
}
catch (e) {
alert(e.message);
}
window.setTimeout(updateDownloadProgress, 200);
}
}
xmlReq.open("GET", urlToFile, true);
xmlReq.onreadystatechange = function(){
if (xmlReq.readyState == 3) {
updateDownloadProgress();
}
if (xmlReq.readyState == 4) {
alert("done");
}
}
xmlReq.send(null);
} catch(e) {
alert(e.message);
return null;
}
}
}
【问题讨论】:
-
如果微软曾经给你这个功能,它会像这样xkcd.com/612 :)
标签: internet-explorer xmlhttprequest javascript