我现在使用Electron构建了一个GUI。 (如PhoneGap的桌面应用程序)Electron:获取上传文件的完整路径
有没有办法启用文件检查的完整路径<input type="file">
?
立即拒绝C:\fakepath\dataset.zip
。 (目录名称不是“fakepath”,但值是document.getElementById("myFile").value
)
或者,还有其他方法可以选择文件吗?
我现在使用Electron构建了一个GUI。 (如PhoneGap的桌面应用程序)Electron:获取上传文件的完整路径
有没有办法启用文件检查的完整路径<input type="file">
?
立即拒绝C:\fakepath\dataset.zip
。 (目录名称不是“fakepath”,但值是document.getElementById("myFile").value
)
或者,还有其他方法可以选择文件吗?
根据这个回答How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?,你不可能为了安全原因去做你正在尝试的东西。
但是,您可以像我在我所从事的电子项目中那样做一项工作。
然后在渲染过程创建一个事件侦听到您之前创建的按钮。
const ipc = require('electron').ipcRenderer;
const buttonCreated = document.getElementById('button-created-id');
buttonCreated.addEventListener('click', function (event) {
ipc.send('open-file-dialog-for-file')
});
然后在主要工艺使用showOpenDialog
选择一个文件,然后发送full path
回到渲染过程。
ipc.on('open-file-dialog-for-file', function (event) {
if(os.platform() === 'linux' || os.platform() === 'win32'){
dialog.showOpenDialog({
properties: ['openFile']
}, function (files) {
if (files) event.sender.send('selected-file', files[0]);
});
} else {
dialog.showOpenDialog({
properties: ['openFile', 'openDirectory']
}, function (files) {
if (files) event.sender.send('selected-file', files[0]);
});
}});
然后在渲染过程你得到的full path
。
ipc.on('selected-file', function (event, path) {
console.log('Full path: ', path);
});
因此,你可以拥有比输入类型的文件类似的行为,并得到完整路径。
*“出于安全原因不可能做你正在尝试的事情”* - 对于最终用户浏览器而言,这是真的,但对于Electron来说则是如此。我相信你可以简单地使用'input.files [0] .path',这在[当前接受的答案](http://stackoverflow.com/a/38549837/2788872)中也有描述。 –
<script>const electron = require('electron');</script>
<button id="myFile" onclick="this.value=electron.remote.dialog.showOpenDialog()[0]">UpdateFile</button>
现在,document.getElementById("myFile").value
将包含所选文件的完整路径。
'C:\ fakepath \ dataset.zip'是一个完整的路径。你什么意思? –
不,目录名称不是'fakepath',但这正是我从document.getElementById(“myFile”).value获得的。 –
你不能得到像这样的输入文件。 –