js上传base64

<!DOCTYPE html>

<html>
<head>
<title>Upload Image</title>
</head>
<body>
<input type="file" id="fileInput">
<button onclick="uploadImage()">Upload Image</button>

<script>
function uploadImage() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];

const reader = new FileReader();
reader.onload = function(event) {
const base64Data = event.target.result;

// 发送 Base64 数据到后端
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://your-backend-url/upload', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ image: base64Data }));
};

reader.readAsDataURL(file);
}
</script>
</body>
</html>

posted @ 2024-07-19 18:12  unique-yb  阅读(1)  评论(0编辑  收藏  举报