Get file size, image width and height before upload

前端 未结 7 1416
自闭症患者
自闭症患者 2020-11-22 03:50

How can I get the file size, image height and width before upload to my website, with jQuery or JavaScript?

7条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 04:17

    Here is a pure JavaScript example of picking an image file, displaying it, looping through the image properties, and then re-sizing the image from the canvas into an IMG tag and explicitly setting the re-sized image type to jpeg.

    If you right click the top image, in the canvas tag, and choose Save File As, it will default to a PNG format. If you right click, and Save File as the lower image, it will default to a JPEG format. Any file over 400px in width is reduced to 400px in width, and a height proportional to the original file.

    HTML

    SCRIPT

    
    

    Here is a jsFiddle:

    jsFiddle Pick, display, get properties, and Re-size an image file

    In jsFiddle, right clicking the top image, which is a canvas, won't give you the same save options as right clicking the bottom image in an IMG tag.

提交回复
热议问题