Trình duyệt dựa trên Firefox và Chromium hỗ trợ download
attribute. Nếu bạn cần khả năng tương thích tốt hơn bây giờ, hãy sử dụng Downloadify dựa trên Flash làm dự phòng.
HTML chỉ: sử dụng download
thuộc tính:
<a download href="http://upload.wikimedia.org/wikipedia/commons/b/bb/Wikipedia_wordmark.svg">Download</a>
Javascript chỉ: bạn có thể lưu bất kỳ tập tin với mã này:
function saveAs(uri) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.setAttribute('download', true);
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
var file = 'http://upload.wikimedia.org/wikipedia/commons/b/bb/Wikipedia_wordmark.svg';
saveAs(file);
Nguồn
2015-03-09 16:22:00
phải đó là những gì tôi nghĩ. –