0

I'm developing an app which has a function for uploading .xls files but everytime I want to test code I have to select the file or drag and drop it to the field

This is the code I use:

const handle_change = (e) => {
    e.stopPropagation()
    e.preventDefault()

    let files

    if(e.type == "drop") {
        files = e.dataTransfer.files
    }else{
        files = e.target.files
    }

    const f = files[0]
    const reader = new FileReader()

    reader.onload = function(e) {
        const data = new Uint8Array(e.target.result);

    ...

Is there anyway to automate this? I mean, when I reload the page it loads the file automatically.

Thank you!

AngelQuesada
  • 387
  • 4
  • 19

1 Answers1

0

You cannot default select a file from your input file to achieve this:

How to set a value to a file input in HTML?

But you skip using the input file value and creating a file to testing it with the File class:

https://developer.mozilla.org/en-US/docs/Web/API/File/File

It will be something like this:

const handle_change = (e) => {
    e.stopPropagation()
    e.preventDefault()

    const f = new File(["foo"], "foo.txt")

    ...
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
fguini
  • 326
  • 1
  • 5