Created
March 20, 2020 21:42
-
-
Save erickzhao/8839edb51e2b11e93a25ae3bb1d83166 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World!</title> | |
<link rel="stylesheet" type="text/css" href="./styles.css"> | |
</head> | |
<body> | |
<button id="save">Save File</button> | |
<script> | |
// You can also require other files to run in this process | |
require('./renderer.js') | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {app, BrowserWindow, dialog, ipcMain} = require('electron') | |
const fs = require('fs'); | |
const http = require('http'); | |
const path = require('path'); | |
function createWindow() { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
nodeIntegration: true | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
// use IPC to communicate from renderer to main | |
// that we want to open a dialog and save the file | |
ipcMain.on('download-file', () => { | |
const filePath = dialog.showSaveDialogSync(mainWindow, { | |
defaultPath: path.join(app.getPath('downloads'), "file.jpg") | |
}); | |
download(filePath); | |
}) | |
} | |
// handles download of file given a path | |
function download(filePath) { | |
const file = fs.createWriteStream(filePath); | |
// hardcoded image for now, can replace with any file | |
http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", | |
(response) => { | |
response.pipe(file); | |
} | |
); | |
} | |
app.on('ready', createWindow) | |
// Quit when all windows are closed. | |
app.on('window-all-closed', function () { | |
// On OS X it is common for applications and their menu bar | |
// to stay active until the user quits explicitly with Cmd + Q | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
app.on('activate', function () { | |
// On OS X it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) { | |
createWindow() | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ipcRenderer } = require('electron'); | |
const button = document.getElementById('save'); | |
button.addEventListener('click', () => { | |
ipcRenderer.send('download-file') | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment