如何使用Ajax发送图像的PHP文件?如何使用、图像、文件、Ajax

2023-09-10 14:13:00 作者:陌上花开又花落

我的问题是,是否有可能将图像上传到服务器使用AJAX(jQuery的)

my question is that is it possible to upload an image to a server using ajax(jquery)

下面是我的AJAX脚本,并且不需要重新加载页面发送文本

below is my ajax script to send text without page reload

$(function() {
//this submits a form
$('#post_submit').click(function(event) {
event.preventDefault();
var great_id = $("#post_container_supreme:first").attr("class");
var poster = $("#poster").val() ;
    $.ajax({
        type: "POST",
        url: "my php file",
        data: 'poster='+ poster + '&great_id=' + great_id,
        beforeSend: function() {
            $("#loader_ic").show();
            $('#loader_ic').fadeIn(400).html('<img src="data_cardz_loader.gif" />').fadeIn("slow");
        },
        success: function(data) {
            $("#loader_ic").hide();
            $("#new_post").prepend(data);
            $("#poster").val('');
        }

    })
})
})

是否有可能修改它发送图像?

is it possible to modify it to send images?

推荐答案

这工作。

$("form[name='uploader']").submit(function(e) {
        var formData = new FormData($(this)[0]);

        $.ajax({
            url: "page.php",
            type: "POST",
            data: formData,
            async: false,
            success: function (msg) {
                alert(msg)
            },
            cache: false,
            contentType: false,
            processData: false
        });

        e.preventDefault();
    });

难道是你正在寻找?

Is it what you were searching for?