weixin_33709590 2017-04-26 05:46 采纳率: 0%
浏览 44

Wordpress AJAX文件上传

I am having trouble saving the uploaded file to the Wordpress database:

Here is the HTML:

<div id="portfolio-container">
<form method="post" id="genealogist-code-upload" enctype="multipart/form-data">
    <div>
        <span class="col-xs-1 check <?php if($locked1 == 0){ echo 'fa fa-times-circle-o'; } else { echo 'fa fa-check-circle'; } ?> "></span>
        <h1 class="col-xs-6">1. Genealogist's Code</h1>
        <?php if($locked1 == 0){ ?>
            <label for="file-field" class="btn add-file-btn col-xs-2 text-center">Add File</label>

            <input type="file" name="file" id="file-field" style="display:none;">
    <!-- </div>
    <div> -->
            <input type="submit" id="1" class="col-xs-2 app-upload-btn">
            <button class="col-xs-1 delete upload-1-delete"><span class="fa fa-trash"></span></button>
        <?php } ?>
    </div>
</form>

Here is the JS:

jQuery(document).ready( function($) {

$('.app-upload-btn').on('click', function(e){
    e.preventDefault();
    $current = $(this);

    // var file_data = $('.file-field').prop('files')[0];
    var file_data = $('#file-field').val();

    var strconfirm = confirm('Are you sure? This cannot be undone.');

    if (strconfirm == true && file_data != undefined) {
        var form_data = new FormData();
        form_data.append('file', file_data);
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            dataType: 'json',
            contentType: false,
            processData: false,
            data: {
                'action': 'save_portfolio',
                'portfolio_num': $(this).attr('id'),
                // 'form_data': form_data
            },
            success: function( data ) {
                $current.siblings(".check").removeClass("fa-times-circle-o").addClass("fa-check-circle");
                $current.css("display", "none");
                $current.siblings('.delete').css("display", "none");
                $current.siblings('.add-file-btn').css("display", "none");
            },
            error: function( error ) {
                console.log('error');
            }
        })
    } else {
        alert('please reupload.');
    }
})

Here is the PHP code where I am having trouble accessing the uploaded file:

function bcg_portfolio_submission() {

$user = wp_get_current_user();
$portfolio_num = $_POST['portfolio_num'];
$portfolio_locked = 'portfolio_locked_'. $portfolio_num;

$form_data = print_r($_FILES['form_data']);

update_user_meta( $user->ID, $portfolio_locked, true);
wp_send_json_success($form_data);

die();

}

add_action( 'wp_ajax_nopriv_save_portfolio', 'bcg_portfolio_submission' );
add_action( 'wp_ajax_save_portfolio', 'bcg_portfolio_submission');

I'd like to send a data object to be accessed via PHP. Can I just add data : form_data as an element of the object? How do I access this form data in php? Do I use the global $_FILE variable? If so, can you provide an example of what this would look like?

  • 写回答

2条回答 默认 最新

  • weixin_33725270 2017-04-26 05:49
    关注

    To upload image using ajax, you have to create a form like:

    var form_data = new FormData();
    form_data.append('file', file_data);
    // In the same way append all the html elements value in the form
    

    now send this form in data like:

    data : form_data
    

    Try with this approach.

    You can get the formdata values in php like:

    $_FILES['file']     // for file
    $_REQUEST['index']  // for other html elements
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器