【报错解决】Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
项目开发日记-bug多多篇(2)
同时也是 实现一些功能(3)
真的痛苦,写一天代码遇到的bug够我写三天博客。
今天是为了做一个头像功能,具体说是用户上传头像文件并且预览的功能。

<div class="col-lg-3" style="border-right-color: #0f0f0f">
<img src="" class="img-circle" id="img">
<input type="file" name="img" id="exampleInputFile" onchange="showImg()" accept="image/gif, image/jpg, image/png">
</div>
HTML

1 <script th:inline="javascript">
2 const exampleInputFile = document.getElementById("exampleInputFile");
3 const img = document.getElementById("img")
4 const fileReader = new FileReader();
5 var imgFile = document.getElementById("exampleInputFile").file;
6 var showImg = function (){
7 fileReader.readAsDataURL(imgFile);
8 console.log(fileReader.result);
9 fileReader.onload = function (){
10 img.innerHTML=fileReader.result;
11 console.log(img.getAttribute("src"));
12 }
13 }
14 </script>
JS
在页面用户提交了头像文件后console报错Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
先记录问题,解决了再来更新。
先说报错原因,在博客园和CSDN逛了一圈都没有找到解决方法,拉踩一下CSDN七、八个热门回答抄的都是同一份作业,不多说。
我去MDN搜索了出错的方法 readAsDataURL(),MDN上对于这一方法的描述为:

大致翻译一下就是:readAsDataURL()这个方法会读取指定的Blob或File文件,读取完后readystate会变成‘DONE’,并且触发loadend事件。同时result属性将会包含一个用于表示文件的URL格式的字符串。
再结合一下报错信息,Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'
那就可以直到了原来Blob是一种文件类型,而不是因为我插入的图片是GGB
意思就是说我们传入readAsDataURL()的参数不是Blob类型的(这里插入一下,file对象是特殊的Blob类型)
那就看看我们传了什么东西进去

其实这里是我按照自己的猜想写的代码,肯定是不规范的,console看看传了什么进去
果不其然,undefined。
那接下来就看看规范的写法应该是怎么样的,我参照了MDN中的代码,这里贴上原链接 https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
我自己的代码:

<script th:inline="javascript">
var showImg = function (){
var img = document.querySelector('img');
const fileReader = new FileReader();
var imgFile = document.querySelector('input[type=file]').files[0];
fileReader.addEventListener('load',function (){
img.src = fileReader.result;
},false);
if (imgFile){
fileReader.readAsDataURL(imgFile);
}
}
</script>
JS
实现效果:

跟预想的差不多,可惜了这个动图。

【报错解决】Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.的更多相关文章
- 【postman】postman测试API报错如下:TypeError: Failed to execute 'fetch' on 'Window': Invalid value 对中文支持不好
使用postman测试APi的时候,因为系统需要在header部带上登录用户的信息,所以 如下: 然后测试报错如下:TypeError: Failed to execute 'fetch' on 'W ...
- Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' ...
- Jquery报错:Uncaught TypeError: ((m.event.special[e.origType] || (intermediate value)).handle || e.handler).apply is not a function
页面中出现了Jquery报错:Uncaught TypeError: ((m.event.special[e.origType] || (intermediate value)).handle || ...
- DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined
DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined 原因:table 中定义的列和aoColumns ...
- jQuery报错:Uncaught TypeError: _this.attr is not a function
问题:想通过延时把置灰的按钮再次复原,代码如下: $("#sendEmailCode").on("click", function() { var _this ...
- JS报错:Uncaught TypeError: Cannot set property ‘nTf‘ of undefined
在使用DataTable时,遇到以下报错: Uncaught TypeError: Cannot set property 'nTf' of undefined ... ... 初步排查后发现是< ...
- Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#<Object>‘的解决方法
发现问题 运行一下以前的一个Vue+webpack的 vue仿新闻网站 小项目,报错 由于自己vue学习不深入,老是这个报错,找了好久(确切的说是整整一下午^...^)才找到原因 -v- Uncau ...
- Maven进行install的时候报错,COMPILATION ERROR : Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.13:test (default-test) on project cmu: There are test failures.
maven进行install的时候,test类里面报错: COMPILATION ERROR : [INFO] -------------------------------------------- ...
- vue 报错解决:TypeError: Cannot read property '_t' of undefined"
前端报错如下: [Vue warn]: Error in render: "TypeError: Cannot read property '_t' of undefined" 是 ...
随机推荐
- FPGA驱动LCD显示红绿蓝彩条
实验目的:先简单熟悉LCD灯的驱动和时序图的代码实现.设计功能是让LCD显示红绿蓝三种颜色,即三个彩带.本次实验比较容易实现,主要是对LCD驱动时序图的理解和时序参数的配置. 实验条件:1.LCD原理 ...
- [使用多仓库解决] idea maven 下载源码出现:Cannot download sources Sources not found for: xxx
根本原因 依赖托管仓库的库存不足.有的仓库,就是没有团队上传这个依赖.所以多加几个镜像源,总有一个仓库能找到. 解决方案 修改 maven 默认配置文件 "C:\Users\<user ...
- ES6中对象新增的方法
属性的简洁表示法 ES6 允许在大括号里面直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = { foo }; console ...
- CentOS6跟CentOS7的区别
1.CentOS6在/etc/profile配置环境变量是JAVAHOME,CentOS7是{JAVA_HOME} 2.
- 分库分表之后分布式如何保证ID全局唯一性
分库分表之后分布式如何保证ID全局唯一性 韩师学子--小倪 2018-07-21 23:35:38 8139 收藏 3分类专栏: Mysql版权 分库分 ...
- Dubbo 使用过程中都遇到了些什么问题?
在注册中心找不到对应的服务,检查 service 实现类是否添加了@service 注解 无法连接到注册中心,检查配置文件中的对应的测试 ip 是否正确
- Spring 的优点?
(1)spring属于低侵入式设计,代码的污染极低: (2)spring的DI机制将对象之间的依赖关系交由框架处理,减低组件的耦合性: (3)Spring提供了AOP技术,支持将一些通用任务,如安全. ...
- css添加全部省略号的方式
https://css-tricks.com/line-clampin/ 框架Clamp:https://github.com/josephschmitt/Clamp.js
- MariaDB数据库设置用户密码
SET PASSWORD [FOR user] = { PASSWORD('some password') | OLD_PASSWORD('some password') | 'encrypted p ...
- 什么是 Redis?
Redis 是完全开源免费的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis 支持数据的持久化,可以 ...