7

For send ajax form i use this code :

<input type="file" class="file_1" id="file" />
  • note : not use "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 not work

Mohammad
  • 169
  • 1
  • 2
  • 12
  • jQuery can't do file uploads by itself. See the [jQuery Form Plugin](http://malsup.com/jquery/form/) – Barmar Nov 16 '13 at 11:41
  • possible duplicate of [How to upload file asnychronously](http://stackoverflow.com/questions/10581126/how-to-upload-file-asynchronously-using-jquery) – Drixson Oseña Nov 16 '13 at 11:42
  • How use ajax for this ?! – Mohammad Nov 16 '13 at 11:44
  • jQuery CAN do files uploads by itself. So many people don't understand how this works, it seems. Google is your friend, really. – Ray Nicholus Nov 16 '13 at 20:19
  • possible duplicate of [How can I upload files asynchronously with jQuery?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) – Ray Nicholus Nov 16 '13 at 20:19

2 Answers2

-1

I think that you can´t. Nevertheless, there are some tools that "emulate" it. Check up this tutorial: http://hayageek.com/ajax-file-upload-jquery/

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

Community
  • 1
  • 1
red_alert
  • 1,738
  • 14
  • 24
-1

You are trying to post a adress name (on the other words you post a string) but file upload needs to be a file. then you can find the answer from the this link:

Uploading both data and files in one form using Ajax?

Community
  • 1
  • 1