基于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>
相关推荐
weixin_4432906917 小时前
【脚本系列】如何使用 Python 脚本对同一文件夹中表头相同的 Excel 文件进行合并
开发语言·python·excel
谢尔登18 小时前
office-ai整合excel
人工智能·excel
djk888818 小时前
.net winfrom 获取上传的Excel文件 单元格的背景色
excel
水...琥珀20 小时前
【日常技能】excel的vlookup 匹配#N/A
excel
流形填表1 天前
AI 助力:如何批量提取 Word 表格字段并导出至 Excel
开发语言·人工智能·word·excel·办公自动化
Smilecoc2 天前
Excel快捷键
excel
开开心心就好2 天前
AI抠图软件,本地运行超快速
网络·人工智能·网络协议·tcp/ip·docker·电脑·excel
努力的小郑2 天前
从 OOM 到秒级导入:EasyExcel 百万级数据优化实战(附可直接跑的工具类)
spring boot·excel
01传说2 天前
JAVA ---Excel高效导入(去重1000万数据对比)
android·java·excel
Lo-Y-eH2 天前
Openpyxl:Python操作Excel的利器
python·excel