AJAX POST使用javascript和PHP上传文件

时间:2016-05-19 23:19:08

标签: javascript php jquery ajax

我正在尝试制作一个简单的文件上传服务,但我需要一种方法来使用AJAX来上传文件,使用JavaScript,jQuery,PHP和AJAX(或者只是其中一些语言)。我已经设置了主页,我想制作它,这样你只能上传一个文件(不是文件夹),它会创建一个随机目录名称,包含6个随机数字和字母。它会将该文件上传到它创建的目录中。我不知道关于AJAX的一件事,比如n00b。请有人帮助我。这是我的文件

的index.php:

<!DOCTYPE html>
<HTML lang="en">
    <head>
        <title>Anonymous File Uploading</title>
        <link rel="stylesheet" href="./css/bootstrap.css">
        <link rel="stylesheet" href="./css/custom.css">
        <script src="./js/jquery.js"></script>
        <script src="./js/bootstrap.js"></script>
    </head>
    <script>
        function upload() {
            if (document.getElementById("file-upload").value == "") {
                alert("Please upload a file first!");
            } else {
                if (!document.getElementById("terms").checked) {
                    alert("You must accept the terms and conditons before you can upload files.");
                } else {
                    jQuery.ajax({
                        beforeSend: function() {
                            status.empty();
                            var percentVal = '0%';
                            bar.width(percentVal)
                            percent.html(percentVal);
                        },
                        uploadProgress: function(event, position, total, percentComplete) {
                            var percentVal = percentComplete + '%';
                            bar.width(percentVal)
                            percent.html(percentVal);
                        },
                        complete: function(xhr) {
                        bar.width("100%");
                        percent.html("100%");
                            status.html(xhr.responseText);
                        }
                    }); 
                }
            }
        }
    </script>
    <body>
        <div class="container top-padded">
            <div class="well">
                <h2 class="header">Anonymous File Uploading</h2>
                <input type="file" class="file-upload" id="file-upload"></input>
                <input type="checkbox"> I agree to the <a href="./terms.txt">terms and conditions</a> of using this site.</input><br>
                <div class="spacer"></div>
                <button class="btn btn-default center-block" onclick="upload();" id="terms">Upload</button>
            </div>
        </div>
    </body>
</HTML>

post.php中:

file blank ( i dont know how to code ajax )

1 个答案:

答案 0 :(得分:0)

我不确定AJAX是否可以上传文件,但你可以用PHP和HTML来处理你所谈论的所有事情,但是你需要花一些时间学习PHP。我会找到很多在线PHP教程之一。

就你想做的事而言,这是你需要学习的内容:

  1. (HTML / PHP)如何上传文件

  2. (PHP)如何创建目录

  3. 这就是它,假设你知道足够的HTML来创建和使用表单。