【问题标题】:image not showing while using reader jquery使用阅读器 jquery 时图像未显示
【发布时间】:2020-06-08 11:23:32
【问题描述】:

图片不显示,,,代码选择图片的相对路径而不是绝对路径

 readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            $('#blah')
                .attr('src', e.target.result.toString);
        };
        reader.readAsDataURL(input.files[0]);
  }

【问题讨论】:

  • 给我们readURL的输入参数的值
  • 我正在使用 html 读取本地文件

标签: javascript html jquery jquery-ui


【解决方案1】:

你有一个小语法错误,toString() 应该作为方法调用:

 function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            $('#blah')
                .attr('src', e.target.result.toString());
        };
        reader.readAsDataURL(input.files[0]);
    }
 }
$('#myFile').on('change', function(){
      readURL(event.target);
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="myFile"/>
<img src="" id="blah" style="height:50px;width:50px;"/>

【讨论】:

  • 我正在使用角度,如果我不使用仍然不输出的 .tostring() 似乎会出现问题
  • 改用e.target.result
  • 它带来了一个错误...我已经检查过了,代码甚至没有通过 if 语句。当我删除 if 语句时,我收到此错误 core.js:6185 ERROR TypeError: Cannot read property '0' of undefined
  • 我在以前的项目中尝试过没有角度的图像,并且图像只显示在 Internet Explorer 上......并且也显示了绝对路径,但在谷歌浏览器和其他浏览器上它没有显示
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多