Benson
Benson

Reputation: 275

open file using jQuery

I try to open a file using jQuery. Here is my HTML,

<button onclick='load();'>load</button>

Here is my js code:

function load() {
  var fileSelector = $('<input id="load" type = "file" multiple />');
  fileSelector.click();
  //code here to get the files ...
}

Now I want to get the loaded files, what should I do?

Upvotes: 0

Views: 18875

Answers (3)

meysam
meysam

Reputation: 1

You can open JSON file with JavaScript and use fetch('json_file patch') .

Upvotes: 0

John Kenneth larbo
John Kenneth larbo

Reputation: 11

Here's my solution I used the file type input and then use the Jquery trigger function to trigger the click event for file input.

$(function(){
  $("#btnFile").click(function(){
    $("#file").trigger("click");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 <input id="file" type="file" hidden/>
 <button id="btnFile">Click Me</button>

Upvotes: 1

Leonardo Delfino
Leonardo Delfino

Reputation: 1498

The HTML5 File Api (http://dev.w3.org/2006/webapi/FileAPI/) allows opening files, however the files must be selected by the User for security.

If you need to open a file without user interaction, then it is necessary to do this on the server side with a language like PHP for example.

Upvotes: 6

Related Questions