<ul dir="auto">
<li>Electron version:1.3.3</li>
<li>Operating system:Windows7</li>
</ul>
<p dir="auto">Why in renderer process the webContents.session Event 'will-download' callbacks params DownloadItem Method setSavePath('path')  it had no effect. But in main process is all right</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="// In the renderer process.
const {BrowserWindow} = require('electron').remote
let win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) =&gt; {
    // Set the save path, making Electron not to prompt a save dialog.
    item.setSavePath('/tmp/save.pdf')
    // Then Electron still pop prompt a save dialog.
})"><pre class="notranslate"><code class="notranslate">// In the renderer process.
const {BrowserWindow} = require('electron').remote
let win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) =&gt; {
    // Set the save path, making Electron not to prompt a save dialog.
    item.setSavePath('/tmp/save.pdf')
    // Then Electron still pop prompt a save dialog.
})
</code></pre></div>
<p dir="auto">Hello,</p>
<p dir="auto"><a href="http://electron.atom.io/docs/all/#downloaditem" rel="nofollow"><code class="notranslate">DownloadItem.setSavePath()</code></a> is supposed to prevent the save dialog, which is exactly what I need. However, this doesn't work in a renderer process. The save dialog still appears. In fact, the <code class="notranslate">"will-download"</code> event handler is called <em>after</em> the save dialog appeared and had been closed. The code was contracted for this issue:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="require(&quot;electron&quot;).remote.getCurrentWebContents().session.on(&quot;will-download&quot;,
        function(event, item, webContents) {
    item.setSavePath(`C:\\Path\\${item.getFilename()}`);
});"><pre class="notranslate"><span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"electron"</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">remote</span><span class="pl-kos">.</span><span class="pl-en">getCurrentWebContents</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">session</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">"will-download"</span><span class="pl-kos">,</span>
        <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">event</span><span class="pl-kos">,</span> <span class="pl-s1">item</span><span class="pl-kos">,</span> <span class="pl-s1">webContents</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">item</span><span class="pl-kos">.</span><span class="pl-en">setSavePath</span><span class="pl-kos">(</span><span class="pl-s">`C:\\Path\\<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">item</span><span class="pl-kos">.</span><span class="pl-en">getFilename</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Although, <em>this does work</em> in allowing you to cancel requests:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="require(&quot;electron&quot;).remote.getCurrentWebContents().session.webRequest.onBeforeRequest(
        function(...) { ... });"><pre class="notranslate"><span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"electron"</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">remote</span><span class="pl-kos">.</span><span class="pl-en">getCurrentWebContents</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">session</span><span class="pl-kos">.</span><span class="pl-c1">webRequest</span><span class="pl-kos">.</span><span class="pl-c1">onBeforeRequest</span><span class="pl-kos">(</span>
        <span class="pl-k">function</span><span class="pl-kos">(</span>...<span class="pl-kos">)</span> <span class="pl-kos">{</span> ... <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Also, <code class="notranslate">DownloadItem.setSavePath()</code> in the main process works as expected. It prevents the save dialog:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let window = new require(&quot;electron&quot;).BrowserWindow();
...
window.webContents.session.on(&quot;will-download&quot;, function(event, item, webContents) {
    item.setSavePath(`C:\\Path\\${item.getFilename()}`);
});"><pre class="notranslate"><span class="pl-k">let</span> <span class="pl-s1">window</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"electron"</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">BrowserWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
...
<span class="pl-s1">window</span><span class="pl-kos">.</span><span class="pl-c1">webContents</span><span class="pl-kos">.</span><span class="pl-c1">session</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">"will-download"</span><span class="pl-kos">,</span> <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">event</span><span class="pl-kos">,</span> <span class="pl-s1">item</span><span class="pl-kos">,</span> <span class="pl-s1">webContents</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">item</span><span class="pl-kos">.</span><span class="pl-en">setSavePath</span><span class="pl-kos">(</span><span class="pl-s">`C:\\Path\\<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">item</span><span class="pl-kos">.</span><span class="pl-en">getFilename</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">}</span></span>`</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Further, having a <code class="notranslate">"will-download"</code> event handler function in both processes basically works, because you can successfully use the <code class="notranslate">DownloadItem</code> in the renderer process to print the download status every second.</p>
<hr>
<p dir="auto">Unfortunately, I couldn't think of a workaround. I don't think there's any possibility to call a function of the renderer process from the main process synchronously and get a result which, then, could be used for <code class="notranslate">setSavePath()</code> to be called from the main process. Is there still some way to get the <code class="notranslate">setSavePath()</code> logic into the renderer process while this bug persists?</p>