基于Base64上传Excel文件

1.导入依赖

java 复制代码
 <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.8.12</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
    </dependency>

2、后端代码

java 复制代码
private static  String fileUrl = "D:/software/nginx-1.23.4/html/";

 public String upload(@RequestBody UploadInfo uploadInfo) {

        String name = uploadInfo.getName();
        String[] imgData = StrUtil.splitToArray(uploadInfo.getBase64(), "base64,");
        byte[] bytes = Base64.decode(imgData[1]);
        name =  IdUtil.fastSimpleUUID() + "_" + name;
        FileUtil.writeBytes(bytes,"D:/software/nginx-1.23.1/html/images/"+name);
       String ext  = FileUtil.getSuffix(name);
 
        switch (ext) {
            case "mp4":
               fileUrl = fileUrl + "video/"+name;        
                break;
            default: ;
               fileUrl = fileUrl + "images/"+name;
                break;
        }


        return "http://img.207.com/images/"+name;
    }

3.前端代码

java 复制代码
const onChange = (uploadFile: any, uploadFiles: any) => {
    uploadData.name = uploadFile.name
    console.log(uploadData.name)
    const file = uploadFile.raw
    let reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => {
        uploadData.base64 = reader.result
        callUploadApi()
    }
}
const callUploadApi = () => {

    uploadApi.upload.call({ name: uploadData.name, base64: uploadData.base64 }).then((res: any) => {

        imageUrl.value = getFileImg(res.data)
    })
}
const getFileImg = (url: string) => {
    let ext = url.split('.')[url.split('.').length - 1]
    switch (ext) {
        case "xlsx":
            return "data:image/png;base64,xxxx"
            break;
        default:
            return url
    }
}
</script>

4.使用原生javascript

java 复制代码
<input type="file" onchange="upload(event)">
<script>
    function upload(e) {
        let file = e.target.files[0];
        let reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
            console.log(reader.result);
        }
    }
</script>
相关推荐
XYX的Blog1 天前
Pandas基础07(Csv/Excel/Mysql数据的存储与读取)
mysql·excel·pandas
东京老树根1 天前
Excel 技巧23 - 在Excel中用切片器做出查询效果(★★★)
笔记·学习·excel
东京老树根3 天前
Excel 技巧22 - Ctrl+D 向下复制(★★),复制同间距图形
笔记·学习·excel
如意机反光镜裸3 天前
如何批量导入竖版Excel表到数据库
数据库·excel
NiNg_1_2343 天前
FastExcel使用详解
开发语言·excel·fastexcel
小奥超人4 天前
EXCEL教程:如何打开Excel隐藏部分?
windows·经验分享·microsoft·excel·办公技巧
Eiceblue5 天前
C# 添加、替换、提取、或删除Excel中的图片
开发语言·c#·excel·visual studio
ynrainy5 天前
Excel分区间统计分析(等步长、不等步长、多维度)
excel
扎量丙不要犟6 天前
excel如何查找一个表的数据在另外一个表是否存在
rust·excel
xun-ming6 天前
Excel中LOOKUP函数的使用
excel·xlookup·vlookup·lookup·hlookup