【问题标题】:Send File image with ajax in wordpress在wordpress中使用ajax发送文件图像
【发布时间】:2021-07-17 03:49:19
【问题描述】:

您好我想通过Ajax在WordPress中发送名称和图像文件等信息,但只发送标题并且图像文件没有保存在$_post中。我也想发几张图。我的代码是:

    var data_image = new FormData();
    $("#add_doc").click(function () {

        var sr = $('#blah').attr('src');
        var title = $('#title_picture').val();
        // console.log(sr);
        $('#theDiv').append('<img width="140px" height="140px" class="attach_file" id="theImg" src="' + sr + '" />')
            .append('<input type="hidden" name="title_file[]" value="' + title + '"> ');


        var props = $('#imgInp')[0].files[0];

        var tile_image = $("#title_picture").val();

        data_image.append(tile_image, props);
        for (var pair of data_image.entries()) {
            console.log(pair[0] + ', ' + pair[1]);
        }


    });

 $("#save").click(function () {


        var data_s3 = new FormData();

        for (var vv of data_image.entries()) {

            data_s3.append("action", "save_data_step_three");

            data_s3.append("title_file",vv[0] );

            data_s3.append("image", vv[1]);
            console.log(vv[1]);

            for (var pair of data_s3.entries()) {
                console.log(pair[0] + ', ' + pair[1]);
            }



            $.ajax({
                url: "/wp-admin/admin-ajax.php",
                "headers": {
                    "Accept":"application/json",           

                },
                "mimeType": "multipart/form-data",
                type: "post",
                data: data_s3,
                cache: false,
                enctype: 'multipart/form-data',
                contentType: false,
                processData: false,
                method: 'POST',

                success: function (response) {
                    console.log(response)
                }
            });

        }
    });

在我发送数据表单之前,我会检查它。一切看起来都是正确的。但是当它发送时,没有收到值。

价值 $_post:

{action: "save", title_file: ""}
action: "save"
title_file: ""

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    您必须使用 PHP 函数中的 wp_upload_bits 函数来上传您的图像。 请参考这个https://artisansweb.net/ajax-file-upload-in-wordpress/

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    猜你喜欢
    • 2014-11-29
    • 2018-07-29
    • 2014-02-05
    • 2014-03-23
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多