0

So, I have created a popup here to load an external JSON file from the system. I want to store that JSON file into one variable in the javascript file. how to achieve this using jquery or javascript?

/* Style this however you like */
#input-file-replacer {
  padding: 3px 5px;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: #ddd;
  color: #333;
  cursor: pointer;
}
<label>
  <input type="file" name="input-name" style="display: none;" />
  <span id="input-file-replacer">Select file</span>
</label>
Shree Batale
  • 227
  • 8
  • 20
  • 1
    Note that, despite the title of the duplicate, nothing is uploaded. It reads the data from the `files` collection of the input using a `FileReader`, exactly as you need to – Rory McCrossan May 04 '20 at 12:24

1 Answers1

1

You need to add a onchange function. Then access the files object inside the function

function uploadFile() {
  let x = document.getElementById("file");
  console.log(x.files)
}
#input-file-replacer {
  padding: 3px 5px;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: #ddd;
  color: #333;
  cursor: pointer;
}
<label>
  <input id ='file' type="file" name="input-name" style="display: none;" onchange="uploadFile()" />
  <span id="input-file-replacer">Select file</span>
</label>
brk
  • 48,835
  • 10
  • 56
  • 78