1

I have the following AJAX function with JQuery:

var formData = $('#FonykerEditForm').serialize();    
$.ajax ({
    type: 'POST',
    url: '<?php echo $html->url('/fonykers/edit',true); ?>',
    data: formData,
    dataType: 'json',
    success: function(response) {
        message.html(response.msg);
        message.fadeIn();
        if(!response.ok) {
            message.removeClass('success');
            message.addClass('error');
        } else {
            message.removeClass('error');
            message.addClass('success');
            username = $('#FonykerUsername').val();
            email = $('#FonykerEmail').val();
        }

        $('#save-account-button').removeAttr('disabled');
        $('.input-text').removeClass('ok');
        $('.input-combo').removeClass('ok');
    },
    error: function (xhr, ajaxOptions, thrownError){
        alert(xhr.statusText);
        alert(thrownError);
        $('#save-account-button').removeAttr('disabled');
    }
});

The problem I'm having is that a type file field in my form is not getting submitted along with the rest of the data of the form, how can I include the file in the data of the ajax request?

3
  • 5
    Files cannot be sent this way using ajax. Use one of the many jquery plugins out there to do uploads. Commented Aug 23, 2011 at 17:08
  • If you really want to send a file via ajax, use HTML5 Commented Aug 23, 2011 at 17:13
  • You could set the target of the form to a hidden iFrame, then listen for the load event of the hidden iframe to see when it returns. You can then get the content from the body of the iframe as the response. Commented Aug 23, 2011 at 17:25

3 Answers 3

9

I tried this link and this works fine for me.

http://portfolio.planetjon.ca/2014/01/26/submit-file-input-via-ajax-jquery-easy-way/

Example:

  $( '#my-form' ).submit( function( e ) {
  $.ajax( {
    url: 'http://host.com/action/',
    type: 'POST',
    data: new FormData( this ),
    processData: false,
    contentType: false
  } );
  e.preventDefault();
} );
Sign up to request clarification or add additional context in comments.

1 Comment

Worked fine for me, thanks. I'm using filestyle so I then ended with .filestyle('clear') and it clears the input after.
2

Like I said in the comment above, sending files via ajax is not straightforward. If you wish to try it anyway. The normal approach I've seen is to create a new iframe, add a file input field to it, select your file and submit it programmatically. This way, the iframe does the submission in the background.

Take a look at how this plugin does it:

https://github.com/valums/file-uploader/blob/master/client/fileuploader.js#L995

https://github.com/FineUploader/fine-uploader

5 Comments

Thanks dude, seems overly complicated for what I want to do, so I guess I'll desist on the ajax submission, the deal is that I'd like for the user to have a preview of the image he is setting and I'm no sure how to do this, since I'm using CakePHP and I'm fairy new to web dev in general so I don't really grasp the structure of how everything goes
Right how I have a field in my user table that saves the path to the image, and in the rest of my project it just gets saved automatically when I submit the form (non AJAX) along with the rest of the form info, so if I upload the image beforehand I'm not sure on how to get the path to it afterwards.
The plugin you use will generally provide a path to the file when it has been uploaded. If not, you can modify the server script to return the path.
@gAMBOOKa your link id dead now
The link is dead, long live the link. Take a look at github.com/valums/file-uploader/blob/master/README.md
1

Basically an AJAX will submit data in the form of key/value pairs.. Since files are binary data, you can't submit files using Ajax.. You'll need to submit the data using a standard form submit instead and on the server since accept a form/multipart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.