ajax文件上传图片获取高度和宽度

ajax file upload image get height and width

本文关键字:取高度 获取 文件 ajax      更新时间:2023-09-26

我希望能够更改使用ajax上传的图像的高度。

这是我的代码:

$(function(){
$("#submitimage").click(function(){
    var data = new FormData(),
        // ClientHeight is not getting the image height it is getting the height of some other element.
        height = $("#file")[0].clientHeight,
        width = $("#file")[0].clientWidth,
        FileName = $("#file").val();
    console.log(height);
    console.log(width);
    FileName = FileName.replace("C:''fakepath''", "");
    data.append('file', $('#file')[0].files[0]);
    $.ajax({
        type: "POST",
        url: "upload_file.php",
        cache: false,
        contentType: false,
        processData: false,
        data: data,
        success: function(data) {
            newimage = "<br><img src='uploadedimages/"+FileName+"' width='"+width+"' height='"+height+"' alt='new image' /><br>";
            $(newimage).appendTo(".current-bulletin");
        }
    });
});

});

我使用console.log来获取它返回的高度和宽度,它总是22x272,所以我相信clientheight正在获取文件按钮的高度或类似的东西。

我可以从那里获得高度吗?还是在加载图像后以某种方式获得图像的高度,然后更改它更好?

所以使用javascript,我仍然没有找到这样做的方法,但我给了图像一个新的img类,并在css中使其具有最大宽度:100%;我能做的最好的。