js 前端实现复制粘贴图片到输入框上传

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title>cv图片</title>
		<style>
			#past-contener {
				border: 1px solid #1d8eff;
				border-radius: 20px;
				min-height: 100px;
				padding: 15px;
			}

			#reset-up {
				height: 50px;
				width: 100%;
				margin-top: 10px;
				border-radius: 20px;
				background-color: #1d8eff;
				color: #ffffff;
				border: none;
				box-shadow: 0px 7px 8px #cfcece;
			}
		</style>
	</head>
	<body>
		<div contenteditable="true" id="past-contener" onfocus="onFocus()" oninput="onInput(event)">粘在这里</div>
		<button id="reset-up" onclick="resetUp(true)">重新上传</button>




		<script>
			const imageInt = document.querySelector("#past-contener");
			const resetBtn = document.querySelector("#reset-up");
			showResetBtn("none") // 隐藏【重新上传】摁钮

			// 监听粘贴框的粘贴事件
			imageInt.addEventListener("paste", function(e) {
				let file = null;
				const items = (e.clipboardData || window.clipboardData).items;
				if (items && items.length) {
					for (var i = 0; i < items.length; i++) {
						if (items[i].type.includes("image")) {
							file = items[i].getAsFile();
							break;
						}
					}
				};
				if (file) {
					console.log("文件:", file);
					showResetBtn("block");
					setTimeout(() => {
						resetUp(false)
					})
					// 此时获取到file文件对象,即可处理上传相关逻辑




				}
			});

			// 重新上传
			function resetUp(bl) {
				imageInt.setAttribute('contenteditable', bl);
				if (bl) {
					onFocus();
					imageInt.focus();
					showResetBtn("none");
				}
			};

			// 【重新上传】摁钮显示控制
			function showResetBtn(value) {
				resetBtn.style.display = value;
			};

			// 对焦清空内容
			function onFocus() {
				imageInt.innerHTML = "";
			};

			// 监听输入
			function onInput(event) {
				if (!event.target.innerHTML.includes("<img src=")) {
					onFocus();
					alert("请粘贴图片")
				}
			}
		</script>
	</body>
</html>
在HTML5中,由于浏览器的安全限制,直接操作本地文件系统(包括复制和粘贴文件夹)通常是受限的,因为这涉及到用户隐私和数据安全。不过,你可以通过一些间接的方式来模拟这个功能: 1. **使用FileReader API**:可以读取文件的内容,然后让用户手动复制到剪贴板。当用户需要粘贴时,他们可以在另一个输入框或其他支持粘贴的地方行。 ```javascript function copyFolderFiles(file) { var reader = new FileReader(); reader.readAsDataURL(file); reader.onloadend = function() { window.clipboardData.setData('text', 'data:image/' + file.type + ';base64,' + reader.result); // 尝试设置剪贴板内容 }; } // 示例用法 document.getElementById('folderButton').addEventListener('click', function() { // 获取文件夹元素 var folderInput = document.getElementById('folderInput'); if (folderInput.files.length > 0) { copyFolderFiles(folderInput.files[0]); } }); ``` 2. **使用第三方库**:有些库,如`clipboard.js`,可以帮助你在前端更方便地处理复制和粘贴操作。 ```html <script src="clipboard.min.js"></script> ... <button id="copyButton">Copy</button> <input type="file" id="folderInput" multiple> <script> const copyButton = document.getElementById('copyButton'); copyButton.addEventListener('click', function() { const files = document.getElementById('folderInput').files; if (files.length > 0) { new ClipboardJS('#copyButton') // 初始化剪贴板插件 .on('success', function(e) { console.log('已复制文件夹到剪贴板'); }) .copy(files[0]); // 执行复制操作 } }); </script> ``` 请注意,以上方法都不能真正实现自动复制整个文件夹的功能,因为浏览器限制了对文件系统的直接操作。用户仍需要手动介入并复制文件内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

美酒没故事°

谢谢看官

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值