How can I display a Save As dialog in an Electron App?

前端 未结 3 2489
野趣味
野趣味 2021-02-19 04:34

I am writing a NodeJS Electron App to be distributed on all platforms. I have a download button that I would like to pop open a Save As dialog with the file being provided from

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 05:07

    Below is an example if you want to use renderer only. On click Electron will show standard browser's Save As dialog. No need of remote nor fs.

    
    
    

    In the renderer javascript file:

    // renderer javascript file
    function saveFile() {
        const content = "File content to save";
        const element = document.createElement("a");
        const file = new Blob([content], {type: "text/plain"});
        element.href = URL.createObjectURL(file);
        element.download = "file.txt";
        element.click();
    }
    

    (I'm on mac and haven't tried it on a Windows machine)

提交回复
热议问题