Gautam Kumar
Gautam Kumar

Reputation: 983

Prompt a download location instead of downloading directly

I am creating a CSV element in JavaScript and then simulating a click to download the respective file.

But instead of downloading directly i want it to open a download prompt to choose the location of file to be downloaded.

var csvString = Papa.unparse(result,{
                                quotes: false,
                                delimiter: ",",
                                newline: "\r\n"
                        });
var a         = document.createElement('a');
a.href        = 'data:attachment/csv,' + escape(csvString);
a.download    = "download.csv";
a.click();

How can this be done?

Upvotes: 15

Views: 13544

Answers (2)

Pramod Karandikar
Pramod Karandikar

Reputation: 5329

This is a browser specific setting.

  1. In Chrome: Go to Settings > Downloads > and then select checkbox Ask where to save each file before downloading
  2. In Edge: Go to Settings > Downloads > and then enable Ask me what to do with each download
  3. In Firefox: Go to Tools > Options, open General tab and select radio button Always ask me where to save files
  4. In Internet Explorer 8: When the download dialog opens up, click Save button and choose the location.

Upvotes: 19

nhaggen
nhaggen

Reputation: 443

You will have to use the File System Access API which is not supported by all browsers but seems to be coming. If you are developing something for "internal use", you might be good with that. If you are developing for the big public, then you might still implement it, but along with it put some try-catch to fall back to the default download.

Read this answer to a similar question for further information.

Upvotes: 2

Related Questions