admin管理员组文章数量:1399217
I use an <a>
element to download a file in HTML page.
It takes some seconds, so I want to show a loading by listening to the http response.
Is there a way to show loading when clicked <a>
element, and close loading when download pleted?
This is what I want:
<a href="https://..." download>download file</a>
I use an <a>
element to download a file in HTML page.
It takes some seconds, so I want to show a loading by listening to the http response.
Is there a way to show loading when clicked <a>
element, and close loading when download pleted?
This is what I want:
<a href="https://..." download>download file</a>
Share
Improve this question
edited Jun 12, 2019 at 7:05
wasanga7
2421 gold badge3 silver badges18 bronze badges
asked Jun 12, 2019 at 6:15
edereder
732 silver badges10 bronze badges
3
- I think you can look at this – shrys Commented Jun 12, 2019 at 6:17
- Possible duplicate of Progress Bar (Download) Using HTML 5 – Justinas Commented Jun 12, 2019 at 7:06
- @Justinas But i can't download a file by XMLHTTPRequest.I checked some solutions on stackoverflow.like: download-file.js. So,do i need to send a XMLHttprequest first, then use createObjectURL to create a URL? – eder Commented Jun 12, 2019 at 7:54
2 Answers
Reset to default 5There are two ways :
- Use ajax for listening event or receive data from Back-end :
$(document).ready(function(){
$("button").click(function(){
$.ajax({url: "/getData", success: function(result){
$("#div1").html(result);
}});
});
});
- In addition , you can use socket.io to listen event from Back-end .
At server send event :
socket.emit('sendData', 'Your Data');
At client listen event :
socket.on('sendData', function(data) {
console.log(data); // Your Data
});
XMLHttpRequest provides the ability to listen to various events that can occur while the request is being processed. Beneath those also is progress
It checks if the amount of data that has been retrieved has changed.
var oReq = new XMLHttpRequest();
oReq.addEventListener("progress", updateProgress);
oReq.open("GET", url);
// progress on transfers from the server to the client (downloads)
function updateProgress (oEvent) {
if (oEvent.lengthComputable) {
// DO your Stuff here
} else {
// Unable to pute progress information since the total size is unknown
}
}
Note: You need to add the event listeners before calling open() on the request. Otherwise the progress events will not fire.
本文标签: javascriptHow to listen for the http request by A html elementStack Overflow
版权声明:本文标题:javascript - How to listen for the http request by A html element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744168888a2593682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论