1

I have an array with URLs from checked checkboxes, after this I need to download all checked files on click

array look like [url1,url2,url3]

my script works fine, collecting data from checkboxes and I can show it but I can't do anything to download this files

<script type="text/javascript">
function GetSelected() {
    //Create an Array.
    var selected = new Array();

    //.
    var tbl = document.getElementById("acf-group_62bea885d8d59");

    //Reference all the CheckBoxes in Table.
    var chks = tbl.getElementsByTagName("INPUT");

    // Loop and push the checked CheckBox value in Array.
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            selected.push(chks[i].value);
        }
    }

    //Display the selected CheckBox values.
    if (selected.length > 0) {
        alert("Selected values: " + selected.join(","));
    }
};
</script>

I need to download selected file from ACF repeater field (file) in post.php For now I can only download only one file but I need to bulk download selected enter image description here

2

1 Answer 1

2

Solution to download files from a list of url :

const urls = ['url1', 'url2', 'url3'];

let fetchFile = function(url) {
    return fetch(url).then(res => res.blob());
}

let exportFile = function(file) {
    let a = document.createElement('a');
    a.href = URL.createObjectURL(file);
    a.setAttribute('download', '');
    a.click();
}

for(const url of urls) {
    fetchFile(url).then(file => exportFile(file));
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.