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条回答 默认 最新

      报告相同问题?

      悬赏问题

      • ¥30 VB6.0操作 webview2内核的浏览器如何精确实现网页弹窗处置
      • ¥15 pr导出的视频打不开,提示“缺少编解码器”怎么解决
      • ¥15 html里js获取php参数值不成功,帮改代码
      • ¥20 如何控制ant design的InputNumber组件 最多输入5位小数
      • ¥15 c语言学生基本信息管理系统
      • ¥100 火车头采集器采集求解
      • ¥88 关于#运行时间 时间重叠 和非重叠#的问题,如何解决?
      • ¥15 C语言,密切接触者追踪
      • ¥20 关于计算机网络问题,请附带讲解
      • ¥30 自动识别图像目标并判断