使用 AJAX 上传文件不起作用

马克·津尼·阿兰茨

我正在尝试从olanod 答案重新创建本指南,但对我不起作用。

我想使用 AJAX 上传文件,但我得到一个空的 $_POST:

<form enctype="multipart/form-data">
        <input type="file" name="file"> 
        <br>
        <input type="text" name="as" value="asd">
        <!--<button type='button' class="add_more">Add More Files</button>-->
<input type="button" value="Upload" />
</form>

这是我的脚本(从olanod 答案复制粘贴):

<script>
        $(document).ready(function(){
          /*  $('.add_more').click(function(e){
                e.preventDefault();
                $(this).before("<input name='upfile[]' type='file'/><br>");
            });*/

            $(':button').on('click', function() 
            {
                $.ajax({
                    // Your server script to process the upload
                    url: 'ajax.php',
                    type: 'POST',

                    // Form data
                    data: new FormData($('form')[0]),

                    // Tell jQuery not to process data or worry about content-type
                    // You *must* include these options!
                    cache: false,
                    contentType: false,
                    processData: false,

                    // Custom XMLHttpRequest
                    xhr: function() {
                        var myXhr = $.ajaxSettings.xhr();
                        if (myXhr.upload) {
                            // For handling the progress of the upload
                            myXhr.upload.addEventListener('progress', function(e) {
                                if (e.lengthComputable) {
                                    $('progress').attr({
                                        value: e.loaded,
                                        max: e.total,
                                    });
                                }
                            } , false);
                        }
                        return myXhr;
                    },
                });
            });
        });
</script>

正如我所说,我想看看我正在服用什么,这是我的 php 文件的结果:

数组(1) { ["as"]=> string(3) "asd" }

我返回了一个文本字段来确定。

PD:对不起我的英语。希望你能理解我,我会努力的!

马克·津尼·阿兰茨

正如@user2486 所说,

你应该使用 $_FILES 而不是 $_POST – user2486

他是对的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章