Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are my $_FILES? Ajax Uploading

I've built a form to upload images, and processed with Prototype/PHP.

$('image_upload').observe('submit', function() {

     var params = $H();
     params.set('name', $('image_title').value);
     params.set('from', $('from_who').value);
     params.set('upload_file', $('upload_file').value);

     new Ajax.Request('/files/upload_process.php', {
      method:'post',
      parameters: params,

      onSuccess: function(r) {
       $('uploadbox').update('<img src="/images/interface/thankyou.png" />');


      }

     })



    });

The form itself sends the data to the server, but when I try to output print_r($_FILES['upload_file']); nothing appears, not even an empty array.

If I output print_r($_POST), the parameters are sent properly, but only the file name of the image.

So it seems the files themselves are not being sent along. How do I handle this? Thanks Rich

like image 860
Richard Testani Avatar asked Apr 12 '10 02:04

Richard Testani


People also ask

Can we upload file using AJAX?

File upload is not possible through AJAX. You can upload file, without refreshing page by using IFrame .

How files are handled in AJAX?

A component must exist on the server to handle the file upload and save the resource locally; The server must send a response to the browser indicating the JavaScript file upload was successful; and. The client's browser must provide an Ajax-based response indicating the file uploaded successfully.


2 Answers

I don't believe you can send files via ajax for security reasons. You'll have to post the form to an iframe and handle it from there.

like image 61
Mike B Avatar answered Sep 22 '22 13:09

Mike B


You cannot use XHR to upload files.
You need either to use an IFRAME or flash/Java and such plugins of the browser.

like image 35
Itay Moav -Malimovka Avatar answered Sep 21 '22 13:09

Itay Moav -Malimovka