2

I'm trying to upload a file using Ajax, but I'm having troubles handling the file... For test purposes I've build a simple code that looks like this:

JS:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_POST["q"];
echo $q;

It works fine and xmlhttp.responseText prints [object File].

My problem, however, is that I need to get the temporary file name with $_FILES["q"]['tmp_name']. To do so I have changed the code to the following:

JS:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("enctype","multipart/form-data");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_FILES["q"]["tmp_name"];
echo $q;

Problem is that now with xmlhttp.responseText I don't get anything. Anyone knows what I'm doing wrong?

don
  • 2,942
  • 11
  • 36
  • 59
  • do a var_dump on $_POST["q"] and see what your result it. The answer may be there. – phpisuber01 Oct 23 '12 at 14:48
  • @Robert checking with var_dump it `returns string(13) "[object File]"` when using $_POST (i.e. when it returned `[object File]`), while it returns `NULL` when using $_FILES (i.e. when it returned nothing). So it means I'm stuck? – don Oct 23 '12 at 15:06

2 Answers2

1

Check out this answer for making file uploads with AJAX. It is possible, but not compatible in all browsers.

jQuery Upload Progress and AJAX file upload

--

Alternatively, if you want on the fly uploads, there is a cool library you can get called 'Uploadify'. It's a flash/jquery (or HTML5 now) rig that allows you to upload files on the fly. In the flash version, last time I used it... you can add in callback functions to make it do essentially anything you want.

Some clever javascript could make this work for you.

http://www.uploadify.com/

Community
  • 1
  • 1
phpisuber01
  • 7,006
  • 3
  • 20
  • 26
  • Perfect thank's! I've looked at the other question and realized how to use `formData`. It has solved the problem, now it gets the file with Ajax as I was trying to do. – don Oct 24 '12 at 11:52
0

AJAX doesn't do file uploads. It's not designed for that. The standard workaround is to have the JS code build a hidden iframe and do a standard POST-type upload in that. As such, if you try doing echo $_FILES['q']['error'], you'd probably have gotten 4 for "no file".

Marc B
  • 340,537
  • 37
  • 382
  • 468
  • I've tried with `$_FILES['q']['error']` but still without any response from the server... My problem is that I would like to implement drag and drop to upload a file, isn't it possible anyhow? I actually don't really need Ajax, but for what I've seen around the only option seemed to be Ajax, but then if you say so I'm a bit confused... – don Oct 23 '12 at 15:08
  • This answer may have been valid at the time of posting but may not be completely correct today – Alex Jan 22 '18 at 20:38