sindresorhus / electron-dl Goto Github PK
View Code? Open in Web Editor NEWSimplified file downloads for your Electron app
License: MIT License
Simplified file downloads for your Electron app
License: MIT License
Is it possible to download files with the plugin from angular 2?
Im downloading a bunch of files, and doing some processing on those. Then creating a zip file and sending that to the user. The zipfile is located in getPath('temp') but the content of the file is:
Failed to compile /var/folders/pl/cgw8lyrn03d50cb8c7npxqxr0000gn/T/MYZIPFILE.ZIP: Asked for /var/folders/pl/cgw8lyrn03d50cb8c7npxqxr0000gn/T/MYZIPFILE.ZIP but it was not precompiled!
Error: Asked for /var/folders/pl/cgw8lyrn03d50cb8c7npxqxr0000gn/T/MYZIPFILE.ZIP but it was not precompiled!
at FileChangedCache.getCacheEntryForPath (/Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:218:15)
at /Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:127:41
at Generator.next (<anonymous>)
at step (/Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:27:191)
at /Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:27:437
at new Promise (<anonymous>)
at /Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:27:99
at FileChangedCache.getHashForPath (/Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/file-change-cache.js:177:7)
at /Users/MYUSERNAME/Development/jpd/out/MYAPPNAME-darwin-x64/MYAPPNAME.app/Contents/Resources/app/node_modules/electron-compile/lib/compiler-host.js:298:51
at Generator.next (<anonymous>)
From what i could find, this happens because of a security measure not allowing access to files that isnt a part of the app it self. Is there any workaround for this issue, or should i handle local files a different way?
how to download undirect links ? for example : http://localhost:8080/jpctasks/download/?t=0&d=25
.
it does not work with electron-dl , but it works fine on browser.
here is error:
E:\nodejs\taskdan\node_modules\electron-dl\index.js:34 Uncaught TypeError: Cannot read property 'getType' of null
at listener (E:\nodejs\taskdan\node_modules\electron-dl\index.js:34)
at CallbacksRegistry.apply (E:\nodejs\taskdan\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.…:48)
at EventEmitter.<anonymous> (E:\nodejs\taskdan\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:299)
at emitThree (events.js:116)
at EventEmitter.emit (events.js:197)
Hi, very good lib indeed,
I have got a small feature request, is there an option to stop an ongoing download forcefully.
I am using v1.10.0 of electron-dl.
This is my code where i am downloading one file.
download(DownloadWindow,
MyURL,
{onProgress:function(d){
console.log(d); //prints NaN or Infinity
}
}).then(dl => console.log(dl.getSavePath()))
.catch(console.error);
This download works in macOS but not Windows. I'm receiving NaN
from my onProgress
handler.
HTTP/1.1 200 OK
Content-Type: application/zip
Content-Length: 1147148
Connection: keep-alive
Date: Wed, 21 Feb 2018 01:47:42 GMT
Last-Modified: Sat, 17 Feb 2018 03:27:43 GMT
ETag: "d674353de23b4513908cf46d90cb89a7"
x-amz-version-id: null
Accept-Ranges: bytes
Server: AmazonS3
Age: 148
X-Cache: Hit from cloudfront
Via: 1.1 6efe4cf59648bc53c4136b0291d22d56.cloudfront.net (CloudFront)
X-Amz-Cf-Id: sOTO1CCLML52l5S0rvU-2x7Xem6ilbjugL-r4XrZD0r8aFfsujdVZg==
I had to switch to S3.
Would two listener got the same downloadItem (one who arrived first) if they are attached to the session at the same time? Or, can we ensure the resolved downloadItem is the one of currently download call?
Perhaps in the onProgress
handler? It could receive a second parameter with a details object. The signature could look something like this:
{
receivedBytes,
totalBytes,
item,
}
Further expanding on this idea, it could also include useful info like eta
(estimated time remaining in milliseconds) and bps
(current bytes per second). These would be handy but are less important as they can be calculated outside of the module using receivedBytes
, totalBytes
and a setTimeout
.
Hello,
I'm new in electron community. And I'm having issue using you lib', if you could help me ?
I've done in my main process.
import ElectronDL from 'electron-dl';
ElectronDL();
and now I would like to use it in my renderer process, but I can't figure out, am I missing something ?
<template>
<div>
<button @click="download"> Click ME </button>
</div>
</template>
<script>
import ElectronDL from 'electron-dl';
export default {
name: 'landing-page',
components: {},
methods: {
download() {
const remoteURL = 'http://i.imgur.com/CuVQGg3.jpg';
ElectronDL.download(this.$electron.remote.getCurrentWindow(), remoteURL)
.then((dl) => {
debugger;
console.log(dl.getSavePath());
})
.catch(console.error);
},
},
};
</script>
<style>
</style>
Best regards
Would it be possible to add HTTP Header support in general, or at least a way to add an Authorization header?
Can you upgrade the code to use native JS template literals instead of pupa?
If you want I can help you with that.
Congrats for your work
Using item.getMimeType()
.
I'm trying to download all the files from a gist and I'm having inconsistent behavior. A basic repro case can be found at this repo. My desired behavior would be that I could set a directory for all the files in a gist, but could change the directory on a gist-by-gist basis (so that directory
would change multiple times within the app's lifetime).
The failed behavior manifests as repeated "Save as..." prompts, despite setting the directory
option. If I cancel out of the prompts, my files do not download. Other times, the app will not prompt me, but will download one file (e.g. thumbnail.png
) N times, where N is the number of files in the gist.
I suspect that this line, which calls download
anew each iteration through an array is the root of my problem, but it's unclear to me what the intended call pattern is for my use case.
Hello, I try to use this package for downloading multiple files. I send the ipc download-subtitle
multiple times but electron downloads just 1 (of 4 files, for example).
Renderer:
ipcRenderer.send("download-subtitle", { file, subtitle });
Main:
ipcMain.on("download-subtitle", async (event, args) => {
const downloadLocation = path.dirname(args.file.path);
const filename = args.file.name.replace(/\.[^/.]+$/, "");
const options = {
saveAs: false,
directory: downloadLocation,
filename: `${filename}.srt`
};
const dl = await download(mainWindow, args.subtitle.url, options);
console.log(dl.getSavePath());
});
I am trying to bypass the save dialog box and directly wants to download file to particular location as to have path of the file getting downloaded to be with me.
Code snippet:
const BrowserWindow = require('electron').remote.BrowserWindow;
var download = require('electron-dl');
download(BrowserWindow.getFocusedWindow(),workbook.toDataURL())
.then(function(dl){
console.log(dl.getSavePath());
})
.catch(console.error);
In above code BrowserWindow.getFocusedWindow() is always undefined and getting exception as Cannot read property 'on' of undefined.
Anything I am missing here ?
filename
Type: string
Default: downloadItem.getFilename()
Name of the saved file.
this doesn't work.
Is there a way (or plans for one) to cancel a download in progress?
Is there a possibility that I can give title of dialog for saveAs option
If a user hits cancel in the dialog box. How can we get a trigger on this?
There progress event should provide information on total file size - for example to be able to display in the UI.
UPDATE: Noticed that the download item is passed to onStarted(). Sorry for bothering.
I'm queuing multiple downloads to happen very quickly with code similar to the following
const download = (files: string[]) => {
const directory = path.join(app.getPath('userData'), 'downloadCache');
files.forEach(file => {
download(file, {
saveAs: false,
directory
}
});
}
All files will be downloaded into the folder downloadCache
inside the userData
directory.
The first several files will download as expected, with all remaining downloads going to the default download directory specified in the registration function
Is it possible to return error message/code information (if available) on what caused a download interruption?
I was trying to interact with a server that was returning error 403 because i needed to include the authorization header. However, the error "Error: The download of [filename] was interrupted" was not helpful for debugging what went wrong.
Thanks for any help.
Hi,
Thanks for the awesome package. I was wondering how can I show the progress in the renderer process. I could not find the right syntax to send it via ipcMain. I would appreciate your help.
Regards,
Bardia
Hi
Currently using this code, it downloads the file great but if it does it again it adds it as a new version, not overwriting the existing file. Can this be achieved using this? If so please could you show me a code example on how to do this.
Thanks.
Regards
Chris
It is mentioned in docs that in download function , url is string
download(BrowserWindow.getFocusedWindow(), args.url)
Is there a way to download a given array of files simultaneously ? and have one promise/callback for all
Can anyone help with promise chain to achieve this ?
I asked the question here stackoverflow
Can someone suggest fix for the code ?
Is there an easy way to use this plugin to download files behind oauth?
Hi - nice lib - really usefull!
I am not extremely experienced so would appreciate if you could show an example or explain how to handle the completion event?
I currently have the following in my main.dev.js
file and would like to have some way of handling the completion of the download (instead of opening the folder):
import { app, BrowserWindow } from 'electron';
...
require('electron-dl')({openFolderWhenDone:true});
...
We only want to show progress bar, but not the badge icon. is there a way to disable the badge?
Hello. So I am using electron-dl, and it works fine if the window has focus. However, if the window does not have focus, nothing downloads and the save dialogue appears, which I don't want it to. I attempted to pass saveAs:false into the options, but still this behaviour persists.
This app needs to check for files and if they are present, download the files every hour. Thus, I need the app to not download without prompting, even if the window does not have focus. Is there any way to overcome this? My snippet is below:
ipcMain.on('dl-files', (event, args) => {
return Promise.all(
args.files.map(async (file)=>{
logger.log(file.url)
return await download(mainWindow, file.url,{saveAs:false})
})
)
.then(...)
While downloading multiple files and downloading progress events, the return value will interfere with each other. Is this an BUG of Electron?
download.zip
Click second times after a short while to observe the download progress
download
This library works correctly for same syntax
Promise
.all(['https://download.filezilla-project.org/client/FileZilla_3.34.0_win64-setup_bundled.exe', 'http://the.earth.li/~sgtatham/putty/latest/w32/putty-0.70-installer.msi', 'http://speedtest.ftp.otenet.gr/files/test10Mb.db']
.map(function (x) {
return download(x, 'dist');
})).then(function () {
console.log('files downloaded!');
});
electron-dl:
while using same with electron-dl does not.
Promise returns before downloads
Promise
.all(['https://download.filezilla-project.org/client/FileZilla_3.34.0_win64-setup_bundled.exe', 'http://the.earth.li/~sgtatham/putty/latest/w32/putty-0.70-installer.msi', 'http://speedtest.ftp.otenet.gr/files/test10Mb.db']
.map(function (x) {
return download(BrowserWindow.getFocusedWindow(), x);
})).then(function (x) {
console.log('files downloaded!');
});
https://stackoverflow.com/questions/46102851/electron-download-a-file-to-a-specific-location
i try to use electron-dl
, everything does well, my main.js
code :
ipcMain.on("download", (event, info) => {
console.dir(event);
console.log(info);
info.properties.onProgress = status => {
console.log('progress');
return event.sender.send("download progress", status)
};
download(BrowserWindow.getFocusedWindow(), info.url, info.properties)
.then(dl => {
console.log("dl.getSavePath()", dl.getSavePath());
return event.sender.send('download complete', dl.getSavePath());
}).catch((error)=>{
console.log('error');
console.log(error);
});
});
but when i turn off my wifi, the code console.log('progress');
keep run, never stop.
so is there any way to stop run onProgress function?
It could bee cool to have an upload()
function in this or another module.
This happens when trying to save an image through the context menu. (At least on development, I haven't tried with a packaged executable).
It says the problem is in electron-dl/index.js line 138
"Cannot read property 'session' of undefined"
Electron: 5.0.2
electron-context-menu: 0.12.1
electron-dl version:1.10
electron version:1.7.8
Give me message:
TypeError: Cannot read property 'getType' of null
at Session.listener (/Users/rain/workspace/xd-oa2/xd-oa/node_modules/electron-dl/index.js:34:18)
I found webContents is NULL.
const listener = (e, item, webContents) => {
console.log(webContents) // is NULL
....
}
I call download in this code
ipcMain.on('download-btn', (e, args) => {
download(BrowserWindow.getFocusedWindow(), args.url, { saveAs: true })
.then(dl => console.log(dl.getSavePath()))
.catch(console.error);
})
Can you help me fix this problem, please.
Chrome adds an incrementing (1)
before the file extension in this case, to avoid potentially disastrous data loss.
I am getting this error while downloading XML file.
ipcMain.on('download-file', function (event, data) {
console.log(data);
download(BrowserWindow.getFocusedWindow(), data.url, {
directory: "G:\\Orders\\"
}).then(function (dl) {
console.log(dl.getSavePath());
}).catch(console.error);
});
TypeError: Cannot read property 'webContents' of null
at Promise (G:\fc-electron\node_modules\electron-dl\index.js:125:22)
at Promise (<anonymous>)
at module.exports.download (G:\fc-electron\node_modules\electron-dl\index.js:122:47)
at EventEmitter.<anonymous> (G:\fc-electron\main.js:53:9)
at emitTwo (events.js:125:13)
at EventEmitter.emit (events.js:213:7)
at WebContents.<anonymous> (G:\fc-electron\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:249:13)
at emitTwo (events.js:125:13)
at WebContents.emit (events.js:213:7)
The files on my cdn Server are stored with random filenames. The actual filenames are stored in a database. How can I predefine a filename (that is different from the filename on the server) for the saveAs dialog?
Defining the filename option does not work. The option is ignored and the filename in the URL is used instead.
This is my code snippet:
const { ipcMain } = require('electron');
const { download } = require('electron-dl');
// ... some code to create BrowserWindow etc. ... //
ipcMain.on('file-download', (event, eventPayload) => {
const downloadOptions = {
saveAs: true,
filename: 'differentFilename.txt'
};
download(win, eventPayload.FileURL, downloadOptions)
.then((dl) => {
console.log(dl.getSavePath())
})
.catch(console.error);
});
@sindresorhus have you tried to make the download link from within a webview work? If I load the entire webpage then electron-dl works really great but when I load a remote url inside webview then the downloads dont work.
Can I dynamically change the file path from the renderer to electron-dl? Kind of new to Electron and this whole deal so I'm having trouble figuring some stuff out.
I cannot specify download location, please help. I want it in the music floder.
I am having an issue and cannot seem to find a fix - when I click on a link that I have in the window, the download starts and finishes fine without a save prompt but a new window is open and it is blank. This new window stays there and has to be manually closed. Have others seen this?
@sindresorhus please add an option to display the error message box.
If make more sense allow users to deal with the download errors. I quickly created some code, so you can copy/paste the text without loosing to much time.
LINE 55 ADD:
const showErrorDialog = options.showErrorDialog != null ? options.showErrorDialog : true; // default is true
LINE 108 CHANGE TO:
if (showErrorDialog) {
electron.dialog.showErrorBox(errorTitle, message);
}
README.md ADD
#### showErrorDialog
Type: `boolean`<br>
Default: `true`
Show error dialog
Thank you for your good work on many modules
Broken for <webview>
Any solution for resume download (after app closed for example?)
when use within webview, show this error please help.
i use below codes :
download(BrowserWindow.getFocusedWindow(), url ,{saveAs:true,openFolderWhenDone:true,onProgress:function(pr){
//console.log(pr+" - ");
}})
.then(dl => console.log(dl.getSavePath()))
.catch(console.error);
but i get this error in consol :
E:\nodejs\bireport\node_modules\electron-dl\index.js:37 Uncaught TypeError: Cannot read property 'fromWebContents' of undefined
at listener (E:\nodejs\bireport\node_modules\electron-dl\index.js:37)
at CallbacksRegistry.apply (E:\nodejs\bireport\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry…:48)
at EventEmitter. (E:\nodejs\bireport\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:283)
at emitThree (events.js:116)
at EventEmitter.emit (events.js:194)
listener @ E:\nodejs\bireport\node_modules\electron-dl\index.js:37
apply @ E:\nodejs\bireport\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry…:48
(anonymous) @ E:\nodejs\bireport\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:283
emitThree @ events.js:116
emit @ events.js:194
how can i fix it ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.