By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!// function comapres is parameter true or false
// by that, one of two files is offered as download
function downloadFile(isFile1) {
const file1Url = 'path/to/file1.ext'; // path to file 1
const file2Url = 'path/to/file2.ext'; // path to file 2
// compare which file is requested to be downloaded
const downloadUrl = isFile1 ? file1Url : file2Url;
// create anchor
const a = document.createElement('a');
a.href = downloadUrl;
a.download = '';
// append anchor to dom
document.body.appendChild(a);
// trigger the download
a.click();
// remove anchor from dom...
browser.downloads.download({url: "https://example.org/image.png"})
and have the link to your download. MDN has some very useful information, you can read more about working with files on their website Working with files - Mozilla | MDN.// function comapres is parameter true or false
// by that, one of two files is offered as download
function downloadFile(isFile1) {
const file1Url = 'path/to/file1.ext'; // path to file 1
const file2Url = 'path/to/file2.ext'; // path to file 2
// compare which file is requested to be downloaded
const downloadUrl = isFile1 ? file1Url : file2Url;
// create anchor
const a = document.createElement('a');
a.href = downloadUrl;
a.download = '';
// append anchor to dom
document.body.appendChild(a);
// trigger the download
a.click();
// remove anchor from dom
document.body.removeChild(a);
}
// usage
downloadFile(true); // downloads file 1
downloadFile(false); // downloads file 2
Thank you, i wrote a version of this.You want this behaviour for webpage or console? Anyway, here is simple example. I cant test now, but its atleast close to something that works.
JavaScript:// function comapres is parameter true or false // by that, one of two files is offered as download function downloadFile(isFile1) { const file1Url = 'path/to/file1.ext'; // path to file 1 const file2Url = 'path/to/file2.ext'; // path to file 2 // compare which file is requested to be downloaded const downloadUrl = isFile1 ? file1Url : file2Url; // create anchor const a = document.createElement('a'); a.href = downloadUrl; a.download = ''; // append anchor to dom document.body.appendChild(a); // trigger the download a.click(); // remove anchor from dom document.body.removeChild(a); } // usage downloadFile(true); // downloads file 1 downloadFile(false); // downloads file 2
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.