6

i have a form that contains 3 File inputs.i want to send it via jquery.i tried serialize function in jquery,but i realized that this function don't send file inputs!

here is my form :

<form id="submit_pics" action="#" >

     file 1 : <input name="file1" id="file1" type="file" /><br />
     file 2 : <input name="file2" id="file2" type="file" /><br />
     file 3 : <input name="file3" id="file3" type="file" /><br />

     <br />
     <div>
         <a id="submit" href="javascript:;" ><img  src="uploads/images/button1.png" /></a>
     </div>

</form>

and this is my javascript code :

<script type="text/javascript">

    $(document).ready(function(){

        $("#level3").click(function(event) {

            var str = $("#submit_pics").serialize(); 
            $.ajax({
                type: "POST",
                url: "track.php",
                data: str, 
                success: function(theResponse) {

                                        $('#req_result').html(theResponse);

                                    }

                return false;   
        });

    });

armin etemadi
  • 519
  • 3
  • 8
  • 18

5 Answers5

10

After some research I found that you can do it with:

$.ajax({
    url: 'track.php',
    type: 'POST',
    data: new FormData($("#submit_pics")[0]),
    processData: false,
    contentType: false
}).

I'm Java programmer (Liferay) and in the server I use this to get the File:

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
File File1 = uploadRequest.getFile("file1");
File File2 = uploadRequest.getFile("file2");

I supose something similar exists in php.

Joan
  • 239
  • 4
  • 6
6

Unfortunately you can not upload files through XMLHttpRequest. AJAX doesn’t actually post forms to the server, it sends selected data to in the form of a POST or GET request. Javascript is not capable of grabbing the file from the users machine and sending it to the server

You can use a tricky workaround, posting (without AJAX) the request to an hidden iframe.

Have a look at this TUTORIAL

dmarucco
  • 590
  • 4
  • 11
  • thank you so much buddy for your complete answer,you've helped me so much :) – armin etemadi Aug 28 '10 at 08:20
  • Is this true? This blog says otherwise: http://portfolio.planetjon.ca/2014/01/26/submit-file-input-via-ajax-jquery-easy-way/ – Rosdi Kasim Apr 11 '14 at 08:35
  • 1
    It used to be true, yes. Now most browsers are up to date and allow this. Although, I believe IE still has some protective feature to not post unless the user manually clicks the submit. – Anthony Mason Jul 12 '16 at 18:08
0

You should take a look at the JQuery Form Plugin. With that you can easily programmatically submit any form you have.

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
-1

You can't do it via $.ajax() but you can use this uploadify plugin.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
-1

This code works for me

 _private.saveFile=function(){
      var formElement = $("#formId")[0];
      var _params = new FormData(formElement),
          _url = $(formElement).attr("action");

      _private.postFile(_url,_params,function(data,error){ 
           console.log(data);
           console.log(error);
      });
 }


_private.postFile=function(_urlService,_parameters,callback){
    var _url= _public.getPath()+_urlService;
    _private.httpPostFile(_url,callback,_parameters);
};

_private.httpPostFile=function(_url,callback,_data){

    $.ajax({
        url: _url,
        type: 'POST',
        data: _data,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data, textStatus, jqXHR) {
            callback(data,null);
        },
        error: function(xhr, status, error) {
            console.log('error ',error);
            console.log('status ',status);
            console.log('xhr ',xhr);
            callback(null,error);
        }
    });
}
Jorge Santos Neill
  • 1,635
  • 13
  • 6