【报错解决】Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
项目开发日记-bug多多篇(2)
同时也是 实现一些功能(3)
真的痛苦,写一天代码遇到的bug够我写三天博客。
今天是为了做一个头像功能,具体说是用户上传头像文件并且预览的功能。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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
我自己的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<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" 是 ...
随机推荐
- IC设计基础
一 前言 这一周连续两场线下面试,紧接着又是微信视频面试,从连续三天的面试中,收获颇丰! 存在的问题: 一是对项目细节模糊: 二是IC基础知识薄弱: 具体表现是,在面试过程中,如被问到DDR3和千兆以 ...
- kafka 保证消息被消费和消息只消费一次
1. 保证消息被消费 即使消息发送到了消息队列,消息也不会万无一失,还是会面临丢失的风险. 我们以 Kafka 为例,消息在Kafka 中是存储在本地磁盘上的, 为了减少消息存储对磁盘的随机 I/O, ...
- Spring Boot 的配置文件有哪几种格式?它们有什么区别?
.properties 和 .yml,它们的区别主要是书写格式不同. 1).properties app.user.name = javastack 2).yml app: ...
- @Bean和@Componet区别
无意在两个类上看到了这两个注解,一个使用了@Bean配合@Configuration,一个使用了@Componet.依稀记得这两个注解都是实现以前在xml中<bean xxx/>的功能,但 ...
- List和 Map区别?
一个是存储单列数据的集合,另一个是存储键和值这样的双列数据的集合,List中存储的数据是有顺序,并且允许重复:Map中存储的数据是没有顺序的,其键是不能重复的,它的值是可以有重复的.
- Dubbo 如何停机?
Dubbo 是通过 JDK 的 ShutdownHook 来完成优雅停机的,所以如果使用 kill -9 PID 等强制关闭指令,是不会执行优雅停机的,只有通过 kill PID 时,才 会执行.
- java对象的克隆以及深拷贝与浅拷贝
一.为什么要使用克隆 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能 会需要一个和A完全相同新对象B,并且此后对B任何改动都不会影响到A中的值,也 ...
- Spring 支持的 ORM?
Spring 支持以下 ORM:HibernateiBatisJPA (Java Persistence API)TopLinkJDO (Java Data Objects)OJB
- 学习ELK日志平台(五)
ELK Stack 通常情况下: 1,开发人员是不能登录线上服务器查看日志信息 2,各个系统的日志繁多,日志数据分散难以查找 3,日志数据量较大,查询速度慢,数据不够实时性 4,一个调用会涉及到多个系 ...
- Numpy怎样将数组读写到文件
Numpy怎样将数组读写到文件 本文档介绍的是Numpy以自己内建二进制的方式,将数组写出到文件,以及从文件加载数组: 如果是文本.表格类数据,一般使用pandas这个类库做加载和处理,不用numpy ...