当前位置:网站首页>在上传之前预览图像
在上传之前预览图像
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);
});边栏推荐
- 洛谷-线段覆盖-(区间排序问题总结)
- 滤波及失真
- 基于Hough变换的直线检测(Matlab)
- conda报错:json.decoder.JSONDecodeError:
- Mobile phone \ landline call forwarding setting method
- 2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览
- 功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY
- Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
- 留存收益率计算公式
- Summary of common interview questions on computer network, including answers
猜你喜欢

记一次invalid bound statement xxxxxx 问题解决思路
![[question] browser get request with token](/img/95/f0b7186f930c014495414c3fcdaa6e.png)
[question] browser get request with token

【HCIA安全】NAT网络地址转换

How to use multiple languages in a project?
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro

Browser browser cache

SPI配置

ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking

【打新必读】工大科雅估值分析,供热节能产品
随机推荐
The hardest lesson we learned from the crypto Market
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
[problem] process the set [','] into (',')
[hcie security] dual computer hot standby - primary and standby backup
js中join方法
[ffmpeg] add timestamp summary to video files
Some unexpected bug records
IT系统为什么需要可观测性?
2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览
SPI configuration
In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift
Niuke brush questions - MySQL series
Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
[hero planet July training leetcode problem solving daily] 26th and check the collection
Computer professional interview topic summary, general navigation
Summary of common interview questions on computer network, including answers
[Oracle training] - deploy Ogg known as zero downtime migration
golang版本管理gvm
ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一