#how to get absolute path of a file being uploaded to my electron app

1 messages · Page 1 of 1 (latest)

dull merlin
#

title. file.path is undefined

wispy cliff
#

Have you tried file[0].path ?

By default input file is array, so you need to pass index of file you want to read

dull merlin
#

event.target.files[0].path

wispy cliff
#

That line means nothing

#

Send code snippet with function that handles file upload

dull merlin
#

sorry i was on phone

#

im back

wispy cliff
#

Also send html with it

dull merlin
#
                                <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));
    };
wispy cliff
#

have you tried to add name to your input?

#

other than that, I can not spot mistake straight away

dull merlin
wispy cliff
#

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]

dull merlin
#

Oh wait it might be ?[0] or smth Idk I haven't used js in years

dull merlin
wispy cliff
#

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

dull merlin
#

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

dull merlin
#

i removed it anyways and got th esame thing

#

this is what file is

wispy cliff
#

Didn't know that, I've been working with js and jquery for some time now 😅

dull merlin
#

so why is path not part of the file object

dull merlin
#

ok apparently its no longe rlike that

#

i have to do this in the preload context