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

时间:2012-08-02 18:28:33

标签: javascript jquery ajax file-upload

我希望能够更改使用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正在获取文件按钮高度或类似的东西。

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

1 个答案:

答案 0 :(得分:0)

所以使用javascript我仍然没有找到这样做,但是我给了图像一个类new-img并且在css中它有一个max-width:100%;我能做的最好。