当前位置:网站首页>在上传之前预览图像
在上传之前预览图像
2022-07-26 20:31:00 【紫微前端】
我们为允许选择图像的文件输入和img用于预览所选文件的元素准备标记。
<input type="file" id="fileInput" />
<img id="preview" />这两个元素都可以通过以下getElementById()方法获取:
const fileEle = document.getElementById('fileInput');
const previewEle = document.getElementById('preview');1. 使用 URL.createObjectURL() 方法
fileEle.addEventListener('change', function (e) {
// Get the selected file
const file = e.target.files[0];
// Create a new URL that references to the file
const url = URL.createObjectURL(file);
// Set the source for preview element
previewEle.src = url;
});2. 使用 FileReader 的 readAsDataURL() 方法
fileEle.addEventListener('change', function (e) {
// Get the selected file
const file = e.target.files[0];
const reader = new FileReader();
reader.addEventListener('load', function () {
// Set the source for preview element
previewEle.src = reader.result;
});
reader.readAsDataURL(file);
});边栏推荐
- Valley segment coverage - (summary of interval sequencing problem)
- Monitor MySQL based on MySQL exporter
- event.preventDefault VS return false
- TypeScript中的类型断言
- [HCIA security] bidirectional nat
- 牛客刷题——Mysql系列
- 银河证券场内基金低佣金开户靠谱吗,可靠安全吗
- Modify excel default code
- Soft test --- database (1) database foundation
- idea中设置核心配置文件的模板
猜你喜欢

【打新必读】工大科雅估值分析,供热节能产品

Practice of microservice in solving Library Download business problems

LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积

2022-7-26 the seventh group of abstractions and interfaces

Deepfake pinches his face. It's hard to tell whether it's true or false. Tom Cruise is more like himself than himself!

2022-7-26 第七组 抽象和接口

idea中检索此方法中有哪些参数以便我们使用——1.类图。2.双击shift

1-《PyTorch深度学习实践》-线性模型

【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复

idea中debug时如何进入指定的用户方法体中?
随机推荐
Web3.0 时代,基于P2PDB实现一款Dapp的技术理论
【HCIA安全】双向NAT
Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
kubernetes之Deployment
CFdiv1+2-Pathwalks-(树状数组+线性dp)
idea中debug时如何进入指定的用户方法体中?
How to configure the legendary SF lander to automatically read the list without a network
Error in render: “TypeError: data.slice is not a function“
基于Hough变换的直线检测(Matlab)
golang版本管理gvm
The hardest lesson we learned from the crypto Market
ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking
2022-7-26 第七组 抽象和接口
Practice of microservice in solving Library Download business problems
Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
[MySQL series] - how much do you know about the index
留存收益率计算公式
有关无线通信的相关内容
js中join方法
Soft test --- database (1) database foundation