当前位置:网站首页>在上传之前预览图像
在上传之前预览图像
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);
});边栏推荐
- Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
- After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores
- Modify excel default code
- 详解西部数据SMR叠瓦式硬盘的190二级编译器(译码表)模块
- Ros2 node communication realizes zero copy
- 牛客多校-Journey-(建图distra+卡常优化)
- Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
- 今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
- Browser browser cache
- The hardest lesson we learned from the crypto Market
猜你喜欢

Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation

我们被一个 kong 的性能 bug 折腾了一个通宵

QT Foundation Day 1 (1) QT, GUI (graphical user interface) development

SPI configuration

Redis interview questions

Set the template of core configuration file in idea

The hardest lesson we learned from the crypto Market
![[must read new] Keya valuation analysis of University of technology, heating energy-saving products](/img/e8/c3ac4e5f11db58031cb9249a4ba0f4.jpg)
[must read new] Keya valuation analysis of University of technology, heating energy-saving products

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

留存收益率计算公式
随机推荐
播报语音 h5 SpeechSynthesisUtterance
Devsecops, speed and security
TypeScript中的类型断言
In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
PLSQL package
使用 LSTM 进行多变量时间序列预测--问题汇总
25张炫酷交互图表,一文入门Plotly
Svn uses fragmented ideas
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
洛谷-线段覆盖-(区间排序问题总结)
Devsecops, speed and security
苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条
【Oracle实训】-部署号称零停机迁移的OGG
Computer professional interview topic summary, general navigation
Cfdiv1+2-pathwalks- (tree array + linear DP)
The hardest lesson we learned from the crypto Market
conda报错:json.decoder.JSONDecodeError:
ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一
CONDA reports an error: json.decoder.jsondecodeerror:
Is it safe for Huishang futures to open an account? What should Huishang futures pay attention to when opening an account?