Upload image using javascript

前端 未结 3 1052
夕颜
夕颜 2020-12-16 01:51

I\'m trying to get image as Object of javascript on the client side to send it using jQuery





        
3条回答
  •  [愿得一人]
    2020-12-16 02:26

    The reason that im.fileSize is only working in IE is because ".fileSize" is only an IE property. Since you have code that works in IE, I would do a check for the browser and render your current code for IE and try something like this for other browsers.

    var imgFile = document.getElementById('submitfile');
    if (imgFile.files && imgFile.files[0]) {
        var width;
        var height;
        var fileSize;
        var reader = new FileReader();
        reader.onload = function(event) {
            var dataUri = event.target.result,
            img = document.createElement("img");
            img.src = dataUri;
            width = img.width;
            height = img.height;
            fileSize = imgFile.files[0].size;
            alert(width);
            alert(height);
            alert(fileSize);
       };
       reader.onerror = function(event) {
           console.error("File could not be read! Code " + event.target.error.code);
       };
       reader.readAsDataURL(imgFile.files[0]);
    }
    

    I haven't tested this code but it should work as long as I don't have some typo. For a better understanding of what I am doing here check out this link.

提交回复
热议问题