通过PHP jQuery的AJAX文件上传 [英] Jquery ajax fileupload via php

查看:178
本文介绍了通过PHP jQuery的AJAX文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的PHP文件上传通过AJAX jQuery中使用该即时通讯。

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

这是我的code在PHP上传文件:

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"];
}

这是我的jQuery阿贾克斯语句:

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);
    });
});

这是我在index.php文件格式:

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.

推荐答案

$()。序列化通常会创建一个 URL-CN codeD 字符串,而忽略文件输入。你必须使用另一种方法。请参阅: http://api.jquery.com/serialize/

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

一种选择是把形式隐藏 IFRAME 键,触发submit事件。

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

另外看看jQuery的文件上传插件: HTTP://blueimp.github。 COM / jQuery的,文件上传/

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

这篇关于通过PHP jQuery的AJAX文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆