开发者

Specify default download folder - possibly with JavaScript?

We're currently in the process of developing a web-based application which will require the downloading of files through the browser. It would be ideal to have these files end up in a specific location on the file system.

Is there a way to make the file save and file open dialog default to a specific folder, such as %USER%\Downloads\MyApp\?

I don't want it to be a forced thing, but if we can get it to default to the desired location, it would at least be a good prompt for the user, and provide a better experience with other portions of the application.

I'm sure this can be done with a Java control, but it would be nice to be able to开发者_Python百科 do it with some simple JavaScript or something instead.


Nope, I'm fairly sure this is not possible using JavaScript on any browser.

The only thing you can suggest is the file's name.


Since you are developing an App for internal use, you might have some influence on the browser that is used. As of spring 2022, Chrome, Opera, Edge support the File-System-Access-API which let you show a openFilePickerDialog or a saveFilePickerDialog which are independent from the default download location or the last file-upload path of the browser.

Once you have selected a file from the desired folder, this location will be stored in your browser preferences (I guess) and the following calls will directly open to this folder.

Take a look at this answer for further reading.

As shown here, it is then possible to specify in which folder the "file-dialog" should start:

const fileHandle = await window.showOpenFilePicker({
  startIn: 'pictures'
});

Instead of picturesyou can specify any "well-known" system folder, or if you program for a well known environment, any path that is likely to be found on the target machines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜