如何使用NeutralinoJS获取输入文件路径?
我的代码:
<input type="file" id="inputFile">const inputFilePath = document.getElementById('inputFile').files[0].path
console.log(inputFilePath)发布于 2021-06-14 01:05:11
我不认为浏览器允许你获取文件路径。
您可以使用文件选择器API来代替os.showDialogOpen(DialogOpenOptions):https://neutralino.js.org/docs/api/os#osshowdialogopendialogopenoptions
<button onclick="onFileUpload()">async onFileUpload () {
let response = await Neutralino.os.showDialogOpen({
title: 'Select a file'
})
console.log(`You've selected: ${response.selectedEntry}`)
}发布于 2021-08-08 08:08:38
为什么需要路径?如果您需要上传文件中的内容,可以通过javascript filereader API获取并使用这些内容。如果您需要该文件供以后使用,您可以通过js filereader读取该文件,然后使用filesystem.writeFile(WriteFileOptions)创建一个新文件并将其保存到您的首选位置(可能是应用程序内部临时路径)。确保目标路径存在。为此,您可以使用filesystem.createDirectory(CreateDirectoryOptions)。
jQuery示例:
jQuery(document).on('change','#myUpload',function(){ //Click on file input
if(jQuery(this).val().length > 0){ //Check if a file was chosen
let input_file = this.files[0];
let file_name = input_file.name;
let fr = new FileReader();
fr.onload = function(e) {
fileCont = e.target.result;
//Do something with file content
saveMyFile(file_name, fileCont); //execute async function to save file
};
fr.readAsText(input_file);
}
});
async function saveMyFile(myFileName, myFileContent){
await Neutralino.filesystem.createDirectory({ path: './myDestPath' }).then(data => { console.log("Path created."); },() => { console.log("Path already exists."); }); //create path if it does not exist
//write the file:
await Neutralino.filesystem.writeFile({
fileName: './myDestPath/' + myFileName,
data: myFileContent
});
}https://stackoverflow.com/questions/67775464
复制相似问题