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

前端 未结 3 2490
野趣味
野趣味 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

    On the HTML button:

    <button onclick='myUrlSaveAs("http://www.example.com/path/to/file.jpg")'>Save As</button>
    

    In your javascript file:

    // Include in the render side
    var elerem = require('electron').remote;
    var dialog = elerem.dialog;
    var app = elerem.app;
    
    var http = require('http');
    var fs = require('fs');
    var path = require('path');
    
    
    function myUrlSaveAs(remoteUrl){
        // app.getPath("desktop")       // User's Desktop folder
        // app.getPath("documents")     // User's "My Documents" folder
        // app.getPath("downloads")     // User's Downloads folder
    
        var toLocalPath = path.resolve(app.getPath("desktop"), path.basename(remoteUrl) 
    
        var userChosenPath = dialog.showSaveDialog({ defaultPath: toLocalPath });
    
        if(userChosenPath){
            download (remoteUrl, userChosenPath, myUrlSaveAsComplete)
        }
    
    
    }
    
    function myUrlSaveAsComplete(err){
        alert("done");
    }
    
    
    function download (url, dest, cb) {
        var file = fs.createWriteStream(dest);
        var request = http.get(url, function(response) {
            response.pipe(file);
            file.on('finish', function() {
                file.close(cb); // close() is async, call cb after close completes.
            });
        }).on('error', function(err) { // Handle errors
            fs.unlink(dest); // Delete the file async. (But we don't check the result)
            if (cb) cb(err.message);
        });
    };
    
    0 讨论(0)
  • 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.

    <!--html-->
    <button onclick="saveFile()">SAVE AS</button>
    

    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)

    0 讨论(0)
  • 2021-02-19 05:08

    Take a look at this page on the electron docs https://github.com/atom/electron/blob/master/docs/api/dialog.md

    There is a section about dialog.showSaveDialog

    Then you can use the URL from the save dialog with a function similar to the one below to save it to that location.

    session.on('will-download', function(event, item, webContents) {
      event.preventDefault();
      require('request')(item.getUrl(), function(data) {
        require('fs').writeFileSync('/somewhere', data);
      });
    });
    

    Found on this page https://github.com/atom/electron/blob/master/docs/api/session.md

    0 讨论(0)
提交回复
热议问题