使用Jquery-Ajax上传文件无法正常工作

时间:2022-04-23 03:20:40

i have 3 file in directory.

我在目录中有3个文件。

1- jquery-1.8.3.min.js

2- index.html

3- addFile.php

and jquery-1.8.3.min.js is main jquery file.
index.html code is :

和jquery-1.8.3.min.js是主要的jquery文件。 index.html代码是:

<html>
    <head>
         <script type="text/javascript" src="jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
                $(document).ready(function () {
                    $("#uploadbutton").click(function () {
                        var filename = $("#tmppic").val();
                        $.ajax({
                            type: "POST",
                            url: "addFile.php",
                            enctype: 'multipart/form-data',
                            data: {
                                file: filename
                            },
                            success: function (result) {
                                alert(result);
                            }
                        });
                    });
                });
        </script>
    </head>
    <body>
        <div>
            <input type="file" name="tmppic" id="tmppic" />
            <input id="uploadbutton" type="button" value="Upload"/>
        </div>
    </body>
</html>

and addFile.php code is :

和addFile.php代码是:

<?php
    //print_r($_FILES["tmppic"]);
    echo $_FILES["tmppic"]["type"];
    move_uploaded_file($_FILES["tmppic"]["tmp_name"], "uploads/" . $_FILES["tmppic"]["name"]);
?>

but this upload file not work.
thanks from helps.

但是这个上传文件不起作用。谢谢你的帮助。

1 个解决方案

#1


1  

I would suggest to use jQuery Form Plugin for ajax file upload.

我建议使用jQuery Form Plugin进行ajax文件上传。

http://www.malsup.com/jquery/form/

#1


1  

I would suggest to use jQuery Form Plugin for ajax file upload.

我建议使用jQuery Form Plugin进行ajax文件上传。

http://www.malsup.com/jquery/form/

相关文章