Implementation
Frontend: Vue Component
A reusable file upload component that converts selected images to base64 format and sends them to the backend.
<template>
<el-avatar
:size="80"
:src="avatarSrc"
@click="showDialog = true"
style="cursor: pointer;"
>
<img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711.png" />
</el-avatar>
<el-drawer v-model="showDialog" title="Upload Image" :with-header="false">
<el-upload
class="avatar-uploader"
:show-file-list="false"
:on-change="handleFileChange"
:auto-upload="false"
>
<el-icon class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</el-drawer>
</template>
<script lang="ts" setup>
import { defineModel, ref } from 'vue';
import { uploadImageApi } from '@/api/modules/upload.ts';
const avatarSrc = defineModel('imageUrl');
const showDialog = ref(false);
const handleFileChange = (file: any) => {
const fileName = file.name;
const fileReader = new FileReader();
fileReader.readAsDataURL(file.raw);
fileReader.onload = (e) => {
sendToServer(fileName, e.target?.result as string);
};
};
const sendToServer = (name: string, dataUrl: string) => {
uploadImageApi.send({
name,
base64: dataUrl
}).then((response: any) => {
avatarSrc.value = response;
showDialog.value = false;
});
};
</script>
<style scoped>
.avatar-uploader .avatar {
width: 100px;
height: 100px;
display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
border: 1px dashed var(--el-border-color);
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 100px;
height: 100px;
text-align: center;
}
</style>
Backend API Service
Spring Boot service that handles base64-encoded file uploads and stores them in the configured directoyr.
package com.example.service;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.example.dto.FileUploadRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class FileUploadService {
@Value("${upload.path}")
private String storagePath;
@Value("${upload.domain}")
private String domainUrl;
public String processUpload(FileUploadRequest request) {
String originalName = request.getName();
String dataUrl = request.getBase64();
// Remove data URL prefix and decode base64
String[] parts = StrUtil.splitToArray(dataUrl, "base64,");
byte[] imageBytes = Base64.decode(parts[1]);
// Generate unique filename with UUID prefix
String uniqueName = IdUtil.fastSimpleUUID() + "_" + originalName;
// Convert Chinese characters to pinyin for better compatibility
uniqueName = PinyinUtil.getPinyin(uniqueName, "");
// Write file to storage directory
FileUtil.writeBytes(imageBytes, storagePath + uniqueName);
// Return accessible URL
return domainUrl + "/images/" + uniqueName;
}
}
API Endpoint
@PostMapping("/upload")
public String handleUpload(@RequestBody FileUploadRequest request) {
return fileUploadService.processUpload(request);
}
Configuration
upload:
path: /var/www/static/images/
domain: https://your-domain.com
Key Points
FileReader API converts file binary data to a data URL string using the readAsDataURL() method, which produces a base64-encoded string prefixed with data:image/type;base64,.
File Naming Strategy uses UUID prefix to prevent collisions and converts Chinese characters to pinyin for cross-platform compatibility.
nginx Configuration serves the static files from the configured upload path for public access.
Usage Example
<template>
<div>
<ImageUploader v-model:imageUrl="userAvatar" />
</div>
</template>
<script setup>
import ImageUploader from '@/components/ImageUploader.vue';
import { ref } from 'vue';
const userAvatar = ref('');
</script>