var csvContent=data; //here we load our csv datavar blob = new Blob([csvContent],{type: "text/csv;charset=utf-8;"});
navigator.msSaveBlob(blob, "filename.csv")
var element = document.createElement('a');element.setAttribute('href', 'data:text/text;charset=utf-8,' + encodeURI(data));element.setAttribute('download', "fileName.txt");element.click();
function(filename,text){// Set up the linkvar link = document.createElement("a");link.setAttribute("target","_blank");if(Blob !== undefined) {var blob = new Blob([text], {type: "text/plain"});link.setAttribute("href", URL.createObjectURL(blob));} else {link.setAttribute("href","data:text/plain," + encodeURIComponent(text));}link.setAttribute("download",filename);document.body.appendChild(link);link.click();document.body.removeChild(link);}
<a href="mp3/tupac_shakur-how-do-you-want-it.mp3" download id="mp3Anchor"><img src="some image that you want" alt="some description" width="100px" height="100px" /></a>
现在使用JavaScript:
*Create a small json file*;
const array = ["mp3/tupac_shakur-how-do-you-want-it.mp3","mp3/spice_one-born-to-die.mp3","mp3/captain_planet_theme_song.mp3","mp3/tenchu-intro.mp3","mp3/resident_evil_nemesis-intro-theme.mp3"];
//load this function on windowwindow.addEventListener("load", downloadList);
//now create a function that will change the content of the href with every clickfunction downloadList() {var changeHref=document.getElementById("mp3Anchor");
var j = -1;
changeHref.addEventListener("click", ()=> {
if(j < array.length-1) {j +=1;changeHref.href=""+array[j];}else {alert("No more content to download");}}
function download(content, mimeType, filename){const a = document.createElement('a') // Create "a" elementconst blob = new Blob([content], {type: mimeType}) // Create a blob (file-like object)const url = URL.createObjectURL(blob) // Create an object URL from bloba.setAttribute('href', url) // Set "a" element linka.setAttribute('download', filename) // Set download filenamea.click() // Start downloading}
function download(filename, text) {var element = document.createElement('a');element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));element.setAttribute('download', filename);
element.style.display = 'none';document.body.appendChild(element);
element.click();
document.body.removeChild(element);}
// Start file download.download("hello.txt","This is the content of my file :)");