The code below does not work, console error:

jquery.min.js: 4 Uncaught TypeError: Illegal invocation "...

What's wrong? datatype not obligatory parameter?

HTML

 <form method="POST"> <input type="file" name="userFile" id="file" value="Выбрать файл"/> <input type="button" onclick="sendFile();" value="test"/> </form> <div id="div_qwe"></div> 

Js

 function sendFile() { $.ajax({ url: "ajax.php", type: "post", data: new FormData($('#file').parents('form:first')[0]), success: function(qwe){ $("#div_qwe").append(qwe); }, error: function (jqXHR, exception) { console.log(jqXHR); console.log(exception); } }); } 

Php

 if($_FILES['userFile']['size']>0) { echo 'Файл принят!'; } 

And if everything is the same but

 data: 'file_xlsx = ' + $('#file').val(), 

Gives an error php file as follows:

Notice: Undefined index: file_xlsx in ajax.php on line 3

  • Look here, the solution has already been found here.stackoverflow.com/questions/381936/… - Dima Krivenko
  • @DimaKrivenko the method you specify is suitable for all file types? For some reason, it does not work for me with xlsx, it gives an error as in the very first question - Vladimir Alexandrov

0