【报错解决】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" 是 ...
随机推荐
- 【Spring AOP】暴力打通两个切面之间的通信
场景描述 在秒杀微服务中,笔者在需要各种校验前端传来的参数后,通过 Redis 加锁限流(切面A)并返回,最后封装订单数据推送到 RabbitMQ 消息队列(切面B)做善后工作. 问题:如何将 切面 ...
- nacos底层原理
Nacos 为什么选择NacosNacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理. Nacos 帮 ...
- 请说说你对Struts2的拦截器的理解?
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 拦截器栈(Interceptor Stac ...
- 解释AOP?
面向切面的编程,或AOP, 是一种编程技术,允许程序模块化横向切割关注点,或横切典型的责任划分,如日志和事务管理.
- 深入理解Java虚拟机-走进Java
一.Java技术体系 从广义上讲, Clojure. JRuby. Groovy等运行于Java虚拟机上的语言及其相关的程序都属于Java技术体系中的一员. 如果仅从传统意义上来看, Sun官方所定义 ...
- request表示HttpServletRequest对象?
request表示HttpServletRequest对象.它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, header, 和session数据的有用的方法. response表示 ...
- resin服务之二----整合resin+Apache服务
整合resin+Apache服务 1.为什么要整合Apache和resin? a. 早期的resin,tomcat对httpd服务支持不好. b. tomcat,resin对rewrite,expi ...
- JDBC和桥接模式
本文参考 网上对于JDBC与桥接模式的理解各有不同,在这片文章里提出的是我个人对于二者的理解,本文参考的其它博文如下: https://blog.csdn.net/paincupid/article/ ...
- MCU选型
含义: MCU(Micro Controller Unit)中文名称为微控制单元,又称单片微型计算机(Single Chip Microcomputer),是指随着大规模集成电路的出现及其发展,将计算 ...
- 论文阅读总结-Patient clustering improves efficiency of federated machine learning to predict mortality and hospital stay time using distributed electronic medical records
一.论文提出的方法: 使用进入ICU前48h的用药特征作为预测因子预测重症监护患者的死亡率和ICU住院时间. 用到了联邦学习,自编码器,k-means聚类算法,社区检测. 数据集:从50家患者人数超过 ...