Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving multiple files #165

Closed
yehudahkay opened this issue Aug 5, 2015 · 6 comments
Closed

Saving multiple files #165

yehudahkay opened this issue Aug 5, 2015 · 6 comments

Comments

@yehudahkay
Copy link

yehudahkay commented Aug 5, 2015

Hi,

I tried saving multiple files in Chrome with

for (i = 0; i < files.length; i++) {
    var blob = new Blob([files[i].data])
    saveAs(blob, files[i].fileName)
}

however it downloads the first file multiple times.

Doing this worked fine

for (i = 0; i < files.length; i++) {
    var blob = new Blob([files[i].data])
    if (navigator.appVersion.toString().indexOf('.NET') > 0)
        window.navigator.msSaveBlob(blob, fileName)
    else {
        var a = document.createElement("a")
        document.body.appendChild(a)
        a.style = "display: none"
        a.href = window.URL.createObjectURL(blob)
        a.download = files[i].fileName
        a.click()
    }
}

Any solution? Thanks!

@melnikaite
Copy link

It works fine in Firefox, but doesn't in Chrome with enabled "Ask where to save each file before downloading"
So I created issue https://code.google.com/p/chromium/issues/detail?id=551457

@Hypercubed
Copy link
Contributor

Yes, I just discovered this. If you try to download multiple files it will only download one. I think it's because the setTimeout here https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js#L146 reuses the save_link element.
.

@melnikaite
Copy link

No, it's because of old issue in chromium https://code.google.com/p/chromium/issues/detail?id=162212
In our project we call saveAs as designed and as fallback show list of links to allow user download other files manually

@Hypercubed
Copy link
Contributor

@melnikaite That may be a separate issue. The code @yehudahkay posted (and the code I tested) loops over several blobs and calls saveAs on each. If you look here: https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js#L146 you will see that the href and download attributes for the save_link element is set then setTimeout is called. By the time the setTimeout callback is invoked href and download have been overwritten multiple times and only the last file is downloaded.

The patch I submitted (Hypercubed@04f232f) fixes this issue by moving the setting of href and download into the setTimeout callback. See here: http://plnkr.co/edit/rVuId9?p=info (saveAs is used indirectly).

@Hypercubed
Copy link
Contributor

Also notice that @yehudahkay working example doesn't use setTimeout.

eligrey added a commit that referenced this issue Dec 10, 2015
minorua added a commit to minorua/WebGISLab that referenced this issue Dec 16, 2015
timoxley added a commit to timoxley/browser-filesaver that referenced this issue Mar 29, 2016
* 'master' of github.com:eligrey/FileSaver.js:
  Removing dead link from demo
  Update demo scripts
  Adding Edge to the browser support table
  Semi-workaround for eligrey#205
  Update sourcecode date
  Fix issue eligrey#207
  Fix eligrey#165, Saving multiple files
  License clarification
  Update version (to go with eligrey#175)
  update dist and readme
  more accurate base64 data processing; moved safari check to variables
  Update dist
  Update dist
  removed encodeURI and fixed Safari detection
  better regex check
  Workaround for downloading application/octet-stream in Safari
  Closes eligrey#164
@endavid
Copy link

endavid commented Feb 5, 2019

This works in Chrome, but it's still not working in Safari. It only downloads the last file. Tested on Safari 12.0.3 (14606.4.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants