- note: do not use the...">

Upload files via jquery $ .post

To submit an ajax form, I use this code:

<input type="file" class="file_1" id="file" /> 
  • note: do not use the "FORM" tag

My jQuery code:

 var file = $('#file').val(); $.post('form.php', {file:file}, function(data){}); 

And my PHP code:

 if($_FILES["file"]["tmp_name"] != '') { $fileName = $_FILES["file"]["name"]; $fileTmpLoc = $_FILES["file"]["tmp_name"]; $fileType = $_FILES["file"]["type"]; $fileSize = $_FILES["file"]["size"]; } echo $fileName; 

But this code does not work

+6
source share
2 answers

I think you canโ€™t. However, there are some tools that imitate it. Check out this tutorial: http://hayageek.com/ajax-file-upload-jquery/

Or this question: Is it possible to use Ajax to upload files?

-1
source

You are trying to publish the address name (in other words, you are sending a string), but the file upload should be a file. then you can find the answer at this link:

Uploading both data and files in one form using Ajax?

-1
source

Source: https://habr.com/ru/post/958195/


All Articles