LoginSignup
1
2

More than 5 years have passed since last update.

ElectronのWebViewを使用したアプリで、ファイルアップロードを実現

Posted at

Electronで<input type="file">に対してファイルをアップロードするファイルを設定することは、
セキュリティ上できないと思っていたが、下記のコードでアップロードすることに成功したのでメモ。
デバッグ機能を活用するみたい。

upload.js
// wv -- WebView
var wc = wv.getWebContents();
try {
    if(wc.debugger.isAttached()) {
        wc.debugger.detach();
    }
    wc.debugger.attach("1.1");
} catch (err) {
    console.error("Debugger attach failed : ", err);
}

wc.debugger.sendCommand("DOM.getDocument", {}, function (err, res) {
  wc.debugger.sendCommand("DOM.querySelector", {
    nodeId: res.root.nodeId,
    selector: "#file"  // CSS selector of input[type=file] element
  }, function (err, res) {
    wc.debugger.sendCommand("DOM.setFileInputFiles", {
      nodeId: res.nodeId,
      files: ['/tmp/tst']  // アップロードしたいファイルパス
    }, function (err, res) {
      wc.debugger.detach();
    });
  });
});

参考
https://github.com/electron/electron/issues/749

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2