#how to get absolute path of a file being uploaded to my electron app
1 messages · Page 1 of 1 (latest)
Have you tried file[0].path ?
By default input file is array, so you need to pass index of file you want to read
Yes
event.target.files[0].path
Also send html with it
<input
type="file"
className="hidden"
id="engine-exe-input"
onChange={handle_exe_upload}
/>
<label
htmlFor="engine-exe-input"
className="cursor-pointer inline-block mt-2 bg-blue-500 text-white py-2 px-4 rounded-lg hover:bg-blue-600 transition-colors h-10"
>
Select
</label>
const handle_exe_upload = (event: ChangeEvent<HTMLInputElement>): void => {
const file = event.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onloadend = async () => {
if (reader.result instanceof ArrayBuffer) {
const bytes = new Uint8Array(reader.result);
const is_executable =
(bytes[0] === 0x4D && bytes[1] === 0x5A) || // Windows PE (MZ)
(bytes[0] === 0x7F && bytes[1] === 0x45 && bytes[2] === 0x4C && bytes[3] === 0x46) || // Linux ELF
((bytes[0] === 0xFE && bytes[1] === 0xED && bytes[2] === 0xFA) || // Mach-O (macOS)
(bytes[0] === 0xCE && bytes[1] === 0xFA && bytes[2] === 0xED) ||
(bytes[0] === 0xCF && bytes[1] === 0xFA && bytes[2] === 0xED));
if (is_executable) {
console.log(window.api.getFilePath(file));
set_selected_exe(file.path);
set_exe_error(false);
} else {
set_exe_error(true);
}
} else set_exe_error(true);
};
reader.readAsArrayBuffer(file.slice(0, 4));
};
have you tried to add name to your input?
other than that, I can not spot mistake straight away
Wdym name
Oh
I see one error
You are reading file index as property instead of index
event.target.files?.[0]
you should go with
event.target.files[0]
Oh wait it might be ?[0] or smth Idk I haven't used js in years
I'll try this tho
when you say
files. (dot) something
it means that you want to read property contained by files object
however
[0] is not prop, it's index that points to first element in files array
i remember you could conditionally chain on an element of an array
But that was 5 yrs ago
I'll check when I get home
yeah i was correct you can conditionally chain like that
i removed it anyways and got th esame thing
this is what file is
Didn't know that, I've been working with js and jquery for some time now 😅
so why is path not part of the file object