wederer
wederer

Reputation: 570

Jquery ajax fileupload via php

i have a problem with my php fileupload which im using via ajax in jquery.

This is my code in the php upload file:

if ($_FILES["file"]["error"] > 0)
{
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
    move_uploaded_file($_FILES["file"]["tmp_name"],"termine.csv");
    echo "Stored in: " . $_FILES["file"]["name"];
}

This is my jquery ajax statement:

 $('#upload_csv').click( function() {
    $.ajax({
        url: '/playground/php/updatedates.php',
        type: 'POST',
        data: $('form#csv').serialize(),
    }).done(function(txt) {
            if(txt!='')alert(txt);
    });
});

and this is my form in the index.php file:

<form enctype="multipart/form-data" id="csv" method="POST">
            <div style="float:left">
            <input name="file" type="file" id="file" size="100">
            <br/>
            <input type="submit" id="upload_csv" value="upload csv file">
            </div>
        </form>

The only error message i get is "error" and nothing else.

Hope somebody can help.

Upvotes: 2

Views: 1830

Answers (1)

John Dvorak
John Dvorak

Reputation: 27317

$().serialize always creates a url-encoded string, and ignores file input. You have to use another approach. See: http://api.jquery.com/serialize/

One option is to put the form in a hidden iframe and trigger the submit event.

Also take a look at the jQuery File Upload plugin: http://blueimp.github.com/jQuery-File-Upload/

Upvotes: 2

Related Questions