Electron - Download a file to a specific location

后端 未结 3 1536
滥情空心
滥情空心 2021-01-31 06:33

I need to download a file to a specific location in my Electron program.
I tried implementing this API but failed.
Then I tried implementing the official API, but couldn

3条回答
  •  忘了有多久
    2021-01-31 06:44

    To allow a user to download a file in an Electron application, you need to do the following:

    1. Get either the default session or the session of the user from the partition. See Session

    2. Once you have an instance of the session object, you can then listen for events like will-download which is emitted on Session object when the user clicks on a link to download a file and the file is going to be downloaded.

    3. The will-download event returns the item which is going to be downloaded. This item contains the necessary events (downloaded, failed, paused etc.) and necessary methods (where to save the file) etc.

    Now, regarding the query on How to download a file to C:/folder ?

    You have 2 choices regarding that:

    1. You can either ask the user to set the download location (Default behavior)
    2. You can set the download location for the file using item object, that you get from the event will-download. Use the method setSavePath on the item object.

    If you rather want to set the default download location for all the files, then you can use, setDownloadPath on the session object. Then that will be the default path for that session.

提交回复
热议问题