13

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?

Gautam Kumar
  • 941
  • 2
  • 16
  • 37
  • 1
    You can't. The user gets to choose what happens for different file types. You (the app developer) cannot override his (the user's) choices. –  Dec 17 '14 at 15:28
  • @torazaburo The problem is the user doesn't get to choose. The app developer says the name is 'download.csv' and it gets saved like that without user intervention. – Chet Jan 10 '17 at 20:16
  • That would happen if the user had chosen to save files without prompting. The user needs to reverse that choice; the precise way to do that depends on the operating system. For instance, Chrome has an option in its Settings to "Ask where to save each file before downloading." The user will then be able to choose where to save the file, and/or change the default name. –  Jan 11 '17 at 01:49

2 Answers2

18

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 Firefox: Go to Tools > Options, open General tab and select radio button Always ask me where to save files
  3. In Internet Explorer 8: When the download dialog opens up, click Save button and choose the location.
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
  • 6
    Is there a way to get the browser to prompt the user if their settings are already set to automatic? I have one use case where the user will want to download the file to a specific folder on their local machine. This is different than their regular downloads. Ideally, I'd like to suggest a folder and have the user allow it. This is for a business application, so we would have a high confidence that the suggested folder would exist and the user would allow the save. – Michael Feb 28 '20 at 20:34
1

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.

nhaggen
  • 301
  • 2
  • 8
  • File System Access API requires *https*, you might not notice it as long as you develop on localhost. This means, that even if run on an internal server, you will need a valid certificate or you'll have to add a security exception for the self-signed certificate on every client. – nhaggen Nov 17 '22 at 07:47