How to get the filename from the Javascript FileReader?

前端 未结 3 1609
一整个雨季
一整个雨季 2020-12-10 10:26

I\'m using the Javascript FileReader to load an image in the browser:

e = e.originalEvent;
e.dataTransfer.dropEffect = \'copy\';
this.documentFile = e.dataTr         


        
3条回答
  •  我在风中等你
    2020-12-10 10:54

    I got the filename and filesize through the FileReader this way

    First of all, the reader is a javascript FILE API specification that is so useful to read files from disc.

    In your example the file is readed by readAsDataURL.

    reader.readAsDataURL(this.documentFile);
    var name = this.documentFile.name;
    var size = this.documentFile.size;
    

    I tried on my site where use this.files[0] instead and worked fine to catch the name and the size with jQuery into an input element.

     reader.readAsDataURL(this.files[0]);
     $("#nombre").val(this.files[0].name);
     $("#tamano").val(this.files[0].size);
    

提交回复
热议问题