'select directory is not working using electron.js
I am using "electron": "^16.0.7", I am trying to select directory by using "webkitdirectory". Whether it select's files instead of selecting folder. And then retrieve the path of file from by using another function. While the "onchange" selects file instead of folders.
The below code creates an element like this,
input.current = document.createElement('input')
input.current.type = 'file'
input.current.webkitdirectory = true
input.current.msdirectory = true
input.current.onchange = (e) => {
const { files } = input.current
watchFiles(files)
input.current.value = ''
}
function watchFiles(files: FileList) {
if (files) {
for (let i = 0; i < files.length; i++) {
const path = files[i] && (files[i].path || files[i].name)
if (path) {
const newId = uniqid()
dispatch(addProject(newId, path))
}
}
}
}
Solution 1:[1]
Here's one method, basically using a hidden utility html element and some jquery methods to weed out any 'tagged' content
const getText = el => {
$('#copy').html($(`${el}`).html());
$('#copy *').each(function() {
$(this).text('')
})
return $('#copy').text().trim()
};
console.log(getText('#LoadRights'))
#copy {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<query id='LoadRights'>
<description>Load all user-rights</description>
SELECT CODE FROM RIGHTS
</query>
<div id="copy"></div>
Solution 2:[2]
you can use camaro for this. note that i use normalize-space() here because i want to trim the newline before and after of the text. remove that if you want the raw text.
const { transform } = require('camaro')
async function main() {
const xml = `
<query id='LoadRights'>
<description>Load all user-rights</description>
SELECT CODE FROM RIGHTS
</query>`
template = {
text: 'normalize-space(query/text())'
}
const output = await transform(xml, template)
console.log(output);
}
main()
output
{ text: 'SELECT CODE FROM RIGHTS' }
In case you have multiple queries, it will look like this
const { transform } = require('camaro')
async function main() {
const xml = `
<queries>
<query id='LoadRights'>
<description>Load all user-rights</description>
SELECT CODE FROM RIGHTS
</query>
<query id='LoadRights'>
<description>Load all user-rights</description>
SELECT CODE FROM RIGHTS 2
</query>
<query id='LoadRights'>
<description>Load all user-rights</description>
SELECT CODE FROM RIGHTS 3
</query>
</queries>`
template = {
queries: ['queries/query', 'normalize-space(text())']
}
const output = await transform(xml, template)
console.log(output);
}
main()
and output will be
{
queries: [
'SELECT CODE FROM RIGHTS',
'SELECT CODE FROM RIGHTS 2',
'SELECT CODE FROM RIGHTS 3'
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kinglish |
| Solution 2 |

