weixin_33749242 2015-08-23 13:23 采纳率: 0%
浏览 25

Ajax POST输入文件到PHP

I'm trying to POST the html input file to my PHP file, instead of using the traditional synchronous html forms.

The issue i'm facing is that i'm don't think im POST'ing the input file correctly to my PHP file because my PHP file is saying that its not receiving any POST data.

HTML:

<form id="uploadForm">
    Begin by uploading a file (<5mb): <br> <br>
    <span class="btn btn-info btn-file" id="buttonColor">
        Browse... <span class="glyphicon glyphicon-folder-open"></span><input type="file" id="uploadBrowseBtn" name="uploadBrowseBtn" onchange='$("#upload-file-info").html($(this).val());'>
    </span>
    <br>
    <span class='label label-info' id="upload-file-info">Choose a file.</span>
    <br> <br>
    <input type="button" class="btn btn-info" id="uploadSubmitBtn" value="Upload">
</form>

JS:

$("#uploadSubmitBtn").click( function() {
    var formData = new FormData($('#uploadForm')[0]);
    $.ajax({
        url: '/upload.php',
        data: formData,
        async: false,
        contentType: false,
        processData: false,
        cache: false,
        type: 'POST',
        success: function(data)
        {

        },
    })
});

PHP:

<?php
    if(empty($_POST)){
        echo true;
    }else{
        echo false;
    }
?>
  • 写回答

1条回答 默认 最新

  • 斗士狗 2015-08-23 14:35
    关注

    PHP puts posted files in the _FILE variable: http://php.net/manual/en/reserved.variables.files.php

    So even if the posting jquery part is working correctly, you could not see if a file was received by checking if the _POST variable is empty in PHP.

    I'm not very good at jQuery, but this should do what you want.

    $( document ).ready(function() {
        $('#uploadSubmitBtn').on('click', function() {
            var file_data = $('#uploadBrowseBtn').prop('files')[0];   
            var form_data = new FormData();
            form_data.append('file', file_data);                        
            $.ajax({
                url: 'upload.php',
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(data){
                    $('#uploadForm').html(data);
                }
            });
        });
    });
    
    <form id="uploadForm">
    <input type="file" id="uploadBrowseBtn">
    <input type="button" id="uploadSubmitBtn" value="Upload">
    </form>
    
    <?php
        var_dump($_POST);   // empty when received a file
        var_dump($_FILES);  // filled when received a file
    ?>
    

    This question: jQuery AJAX file upload PHP answers your question, and has a perfect explanation to what it actually does.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!