douhe6255 2017-09-05 11:06
浏览 98
已采纳

使用AJAX上传文件无法正常工作

i'm trying to recreate this guide from olanod answer but isn't working for me.

I want to upload a file using AJAX but i'm getting an empty $_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>

and this is my script (copy paste from olanod answer ):

<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>

As i said, i'm tring to see what i'm taking and this is the result from my php file:

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

I returned a text field to be sure.

P.D: Sorry for my english. I hope you can understand me, i'm trying my best!

  • 写回答

3条回答 默认 最新

  • doujiao1814 2017-09-06 07:33
    关注

    As @user2486 said,

    You should use $_FILES not $_POST – user2486

    He is right.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵