
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.list { padding: 10px;list-style: none}
.list:after { content: ""; display: block; clear: both; height: 0; line-height: 0; font-size: 0;}
.list li { position: relative; float: left; width: 60px; height: 60px; margin-right: 10px; border: 1px solid #ccc;}
.list li:before { content: ""; position: absolute; top: 50%; left: 50%; width: 30px; height: 1px; margin-top: -1px; margin-left: -15px; background-color: #ccc;}
.list li:after { content: ""; position: absolute; top: 50%; left: 50%; width: 1px; height: 30px; margin-top: -15px; margin-left: -1px; background-color: #ccc;}
.list li img { position: relative; z-index: 2; display: block; width: 100%; height: 100%;}
.list input[type="file"] { position: absolute; left: 0; top: 0; z-index: 3; display: block; width: 100%; height: 100%; cursor: pointer; opacity: 0;}
</style>
</head>
<body>
<ul class="list">
<li>
<input type="file" onchange="handleFiles(this.files,this.parentNode)">
</li>
<li>
<input type="file" onchange="handleFiles(this.files,this.parentNode)">
</li>
<li>
<input type="file" onchange="handleFiles(this.files,this)">
</li>
</ul>
</body>
<script>
//构建预览上传图片的函数,并接收传递过来的2个变量参数
function handleFiles(file,obj) {
console.log(file)
console.log(obj)
//获取当前点击的元素的所有同级元素的html内容
var con = obj.innerHTML;
//判断当前点击元素内是否已经存在img图片元素,如果有则先全部清除后再添加,如果没有就直接添加
if (con.indexOf("img") > 0) {
var pic = obj.getElementsByTagName("img");
for (var i=0; i<pic.length; i++) {
obj.removeChild(pic[i]);
}
//调用添加img图片的函数
creatImg();
} else {
creatImg();
}
function creatImg() {
//创建一个img元素
var img = document.createElement("img");
//设置img元素的源文件路径,window.URL.createObjectURL() 方法会根据传入的参数创建一个指向该参数对象的URL. 这个URL的生命仅存在于它被创建的这个文档里
// img.src = window.URL.createObjectURL(file[0]);
// //window.URL.revokeObjectURL() 释放一个通过URL.createObjectURL()创建的对象URL,在图片被显示出来后,我们就不再需要再次使用这个URL了,因此必须要在加载后释放它
// img.onload = function() {
// window.URL.revokeObjectURL(this.src);
// }
//第二种方式
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// var srcimg = new Image();
var quality = 0.5;
var tmp_srcimgdata = URL.createObjectURL(file[0])
img.src = tmp_srcimgdata;
img.onload = function(){
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height);
var dataUrl = canvas.toDataURL(file[0].type, quality);
//转成Blob对象 FormData上传用
var fs=dataURItoBlob(dataUrl)
var fd = new FormData(document.forms[0]);
fd.append("canvasImage", blob);
console.log(fs)
// console.log(dataUrl);
}
//在当前点击的input元素后添加刚刚创建的img图片元素
obj.appendChild(img);
}
}
//转 Bole函数
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
let byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0){
byteString = atob(dataURI.split(',')[1]);
}else{
byteString = unescape(dataURI.split(',')[1]);
}
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
let ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
// 第二种
function handleFiles(file){
var files =file;
var img = new Image();
var reader =new FileReader();
reader.readAsDataURL(files[0]);
reader.onload =function(e){
var dx =(e.total/1024)/1024;
if(dx>=2){
alert("文件大小大于2M");
return;
}
img.src =this.result;
img.style.width ="100%";
img.style.height ="90%";
document.querySelector('.container').appendChild(img);
}
}
</script
</html>


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构