Open the static HTML page directly in your browser
download
Setting is invalid- Files that can be opened by the browser will be opened directly
FIG. 1
Figure 2
Click download effect:
Open the HTML page with a plug-in on VSCode
Non-cross-domain case:
download
Set up effective- The file will be downloaded normally
Cross-domain cases:
download
Setting is invalid- Resources that can be opened directly by the browser, such as images, are opened directly in the browser instead of being downloaded as files
code
Click download effect:
Cross-domain download images will be directly opened by the browser
a
Tag cross-domain download file implementation
Download the.xlsx file
Code implementation
<body>
<button onclick="download()">Download (Cross-domain)</button>
<script>
function download() {
var url = 'http://scdn.gongyi.qq.com/quiz/Exam-Template.xlsx';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true); / / asynchronous
xhr.responseType = 'blob'; / / blob
xhr.onload = function () {
if(xhr.status ! =200) {
alert('Download exception! ');
return;
}
if (window.navigator.msSaveOrOpenBlob) {
// IE
navigator.msSaveBlob(xhr.response, filename);
} else {
var newUrl = window.URL.createObjectURL(xhr.response);
var a = document.createElement('a');
a.setAttribute('href', newUrl);
a.setAttribute('target'.'_blank');
a.setAttribute('download'.'Template file.xlsx'); // Custom file name (valid)
document.body.appendChild(a);
a.click();
document.body.removeChild(a); }}; xhr.send(); }</script>
</body>
Copy the code
Click download effect:
Download the pictures
Code implementation
<script>.var url = 'https://new.inews.gtimg.com/tnews/d8c3cb72/10f0/d8c3cb72-10f0-4a23-a094-3bfc5b0d60b9.png'; . a.setAttribute('download'.'graffiti. PNG'); .</script>
Copy the code
Click download effect: