Guest, before posting your code please take these rules into consideration:
It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
Don't share a wall of code. All we want is the problem area, the code related to your issue.
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...
You can probably use something like 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.
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
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.