在JavaScript中实现手机图片多选功能,通常涉及HTML5的文件API和DOM操作。以下是一个基本的实现示例:
change
事件,以获取用户选择的文件。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片多选上传</title>
</head>
<body>
<input type="file" id="fileInput" multiple accept="image/*">
<button onclick="uploadFiles()">上传图片</button>
<script>
function uploadFiles() {
const fileInput = document.getElementById('fileInput');
const files = fileInput.files;
if (files.length === 0) {
alert('请选择文件');
return;
}
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('files[]', files[i]);
}
fetch('/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log('上传成功:', data);
})
.catch(error => {
console.error('上传失败:', error);
});
}
</script>
</body>
</html>
通过以上步骤和代码示例,可以实现一个基本的手机图片多选上传功能。根据具体需求,可以进一步优化和扩展功能。
领取专属 10元无门槛券
手把手带您无忧上云