<p dir="auto"><code class="notranslate">&lt;webview&gt;</code> elements are able to detect and/or override many of their guest web pages' actions, such as when guest web pages attempt to close themselves and open new windows. However, there is a need for detecting and overriding another type of guest-page event: when an <code class="notranslate">&lt;input type=file&gt;</code> element in a guest page is activated.</p>
<p dir="auto">For instance, in the Objective-C Apple WebKit framework, the WebUIDelegate method <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Protocols/WebUIDelegate_Protocol/index.html#//apple_ref/occ/instm/NSObject/webView:runOpenPanelForFileButtonWithResultListener:" rel="nofollow"><code class="notranslate">webView:runOpenPanelForFileButtonWithResultListener:</code></a> can <a href="http://stackoverflow.com/questions/5177640/cocoa-webkit-how-to-get-file-upload-file-system-access-in-webkit" rel="nofollow">intercept web pages' attempts to display file-input dialogs</a> and allows the surrounding application to display whatever sort of file dialog it wants instead, passing the chosen files back to the web page.</p>
<p dir="auto">Currently, when an <code class="notranslate">&lt;input type=file&gt;</code> element is activated, Electron will always display the system OS's default file-choosing dialog, with no way for the application to intervene, display an alternate UI, or automatically pass specific files back to the web page. The <code class="notranslate">&lt;webview&gt;</code> element should support overriding file-chooser dialogs.</p>
<p dir="auto">This issue is somewhat related to Electron issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="46982659" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/749" data-hovercard-type="issue" data-hovercard-url="/electron/electron/issues/749/hovercard" href="https://github.com/electron/electron/issues/749">#749</a>. In particular, NW.js, a project alternative to Electron, somewhat obviates the need for this enhancement by allowing <code class="notranslate">&lt;input type=file&gt;</code> elements' files to be directly set. However, it still has the problem of allowing any guest page to open a file-chooser dialog and modally block the application.</p>
<p dir="auto">Perhaps this would ideally be implemented similarly to how Electron has implemented overriding popup windows, but it has to also allow the host application to pass back files to waiting guest web pages—for instance, this could be done by implementing the following:</p>
<ol dir="auto">
<li>Adding an <code class="notranslate">overridefileuploads</code> attribute to <code class="notranslate">&lt;webview&gt;</code>, similarly to <code class="notranslate">allowpopups</code>. This attribute is off by default. When it is off, attempts by the guest page to open a file-upload dialog will occur normally, as they currently do. When it is on, the <code class="notranslate">&lt;webview&gt;</code> will modally wait for the <code class="notranslate">uploadFiles(filePaths)</code> method, defined below, to be called.</li>
<li>Having <code class="notranslate">&lt;webview&gt;s</code> emit a <code class="notranslate">file-dialog</code> event to <code class="notranslate">&lt;webview&gt;</code>, similarly to <code class="notranslate">new-window</code>. If  <code class="notranslate">overidefileupload</code> is on, then this event indicates that its <code class="notranslate">isWaitingForFileUpload()</code> method now returns true.</li>
<li>Adding a <code class="notranslate">isWaitingForFileUpload()</code> method to <code class="notranslate">&lt;webview&gt;</code>. Returns true if the <code class="notranslate">&lt;webview&gt;</code> is waiting for files to be chosen (which can only occur if its <code class="notranslate">overridefileuploads</code> attribute is on). During this mode, the <code class="notranslate">&lt;webview&gt;</code> is frozen and cannot execute commands such as <code class="notranslate">cut()</code>, which will cause it to throw an error (or perhaps it would cancel the file upload, as if the Escape key were pressed while a file-upload dialog was activate).</li>
<li>Adding a <code class="notranslate">uploadFiles(filePaths)</code> method to <code class="notranslate">&lt;webview&gt;</code>. The <code class="notranslate">&lt;webview&gt;</code> must be in waiting-for-files mode. Otherwise, this throws an error (or maybe acts as a no-op, in which case it should return a special value).</li>
</ol>
<p dir="auto">Alternatively, something like <a href="https://github.com/nwjs/nw.js/wiki/Window#user-content-new-win-policy">NW.js's <code class="notranslate">Window</code>'s <code class="notranslate">new-win-policy</code> event</a> could be used.</p>
<p dir="auto">Many of these changes might also be added to <code class="notranslate">webContents</code>, wherever they would make sense.</p>
<p dir="auto">However, whatever the method with which this functionality is implemented, there will remain a need for overriding or at least disallowing guest pages from opening file-choosing dialogs whenever they want to, just as their ability to open popups or close themselves can also be overridden.</p>
<p dir="auto">It could be useful to be able to intercept dialog events from webview (alert, confirm, prompt) to be able to implement our own gui.</p>
<p dir="auto">Also it is part of chrome webview events<br>
<a href="https://developer.chrome.com/apps/tags/webview#event-dialog" rel="nofollow">https://developer.chrome.com/apps/tags/webview#event-dialog</a></p>