最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Specify default download folder - possibly with JavaScript? - Stack Overflow

programmeradmin3浏览0评论

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 do it with some simple JavaScript or something instead.

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 do it with some simple JavaScript or something instead.

Share Improve this question edited Mar 3, 2014 at 12:32 Santosh Panda 7,3418 gold badges44 silver badges57 bronze badges asked Dec 15, 2010 at 19:05 Brian HBrian H 8331 gold badge5 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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)

  1. 暂无评论