Skip to content
14paxton edited this page Feb 13, 2023 · 2 revisions

download a new file

const formV = document.querySelector("input");
const btn = document.querySelector("button");

btn.addEventListener("click" , (e)=>{
    e.preventDefault();
    
    filefetch(formV.value);
})

function filefetch(url){
    fetch(url)
    .then(resp => resp.blob())


    .then(file => {
        let tURL = URL.createObjectURL(file);
        let aTag = document.createElement("a");
        aTag.href = tURL;
        aTag.download = "file name";
        document.body.appendChild(aTag);
        aTag.click();
        aTag.remove();
        URL.revokeObjectURL(tURL);

    })
}

Clone this wiki locally