search

Home  >  Q&A  >  body text

javascript - Why is the image size I get here NaN?

1

2

3

4

5

<code>    <form enctype="multipart/form-data" name="form1">

      <input type="file" name="file">

      <input type="button" value="取得图片的大小" onClick="getSize()">

    </form>

    <img name="pic" alt="在这儿显示图片"></code>

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>function getSize(){

        size = 0;

        imgURL = form1.file.value;

        imgURLsplice = imgURL.split("\").pop();

        imgURLsplicelocal ="../assets/img/" + imgURLsplice;

        img = new Image();

        img.src = imgURLsplicelocal;

        pic.src = imgURLsplicelocal;

        if (size <= 0){

            size = img.fileSize / 1024;

        }

        alert("图片大小为:" + size + "KB");

    }</code>

巴扎黑巴扎黑2860 days ago566

reply all(3)I'll reply

  • 世界只因有你

    世界只因有你2017-05-19 10:44:49

    img.fileSize is a number not a character

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:44:49

    You will know after taking a look at the img.fileSize log.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:44:49

    1

    2

    3

    <code>img是个dom类型<img>的东西啊。怎么可能有fileSize这种东西

    $("#file")[0].files[0].size才可以有

    </code>

    reply
    0
  • Cancelreply