我试图使用Ajax下载文件并显示自定义下载进度条。显示使用XHR2/AJAX下载文件的进度条
问题是我不明白该怎么做。我编写了代码来记录进度,但不知道如何启动下载。
注意:这些文件是不同的类型。
在此先感谢。
JS
// Downloading of files
filelist.on('click', '.download_link', function(e){
e.preventDefault();
var id = $(this).data('id');
$(this).parent().addClass("download_start");
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
// Handle Download Progress
xhr.addEventListener("progress", function (evt) {
if(evt.lengthComputable) {
var percentComplete = evt.loaded/evt.total;
console.log(percentComplete);
}
}, false);
return xhr;
},
complete: function() {
console.log("Request finished");
}
})
});
HTML和PHP
<li>
<div class="f_icon"><img src="' . $ico_path . '"></div>
<div class="left_wing">
<div class="progressbar"></div>
<a class="download_link" href="#" id="'.$file_id.'"><div class="f_name">' . $full_file_name . '</div></a>
<div class="f_time_size">' . date("M d, Y", $file_upload_time) . ' • ' . human_filesize($file_size) . '</div>
</div>
<div class="right_wing">
<div class="f_delete">
<a class="btn btn-danger" href="#" aria-label="Delete" data-id="'.$file_id.'" data-filename="'.$full_file_name.'"><i class="fa fa-trash-o fa-lg" aria-hidden="true" title="Delete this?"></i>
</a>
</div>
</div>
</li>
你怎么送你下载到用户的文件吗? –
如果你问我如何下载文件,那么这就是我想知道我应该如何下载文件。 – Ayan
它看起来像你问如何显示进度,但在第二次阅读,这是一个额外的下载。简单的答案:不要使用ajax来下载文件,使用浏览器。 'download' –