Jquery ajax fileupload via php

送分小仙女□ 提交于 2019-12-11 01:37:53

问题


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.


回答1:


$().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/



来源:https://stackoverflow.com/questions/13306447/jquery-ajax-fileupload-via-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!