基于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>
相关推荐
Morantkk16 小时前
Word和Excel使用有感
word·excel
躺平的花卷17 小时前
Python爬虫案例八:抓取597招聘网信息并用xlutils进行excel数据的保存
爬虫·excel
爱编程的小生17 小时前
Easyexcel(2-文件读取)
java·excel
程序员如山石17 小时前
Excel的图表使用和导出准备
excel
zhy81030219 小时前
.net6 使用 FreeSpire.XLS 实现 excel 转 pdf - docker 部署
pdf·.net·excel
傻啦嘿哟19 小时前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
星星会笑滴1 天前
vue+node+Express+xlsx+emements-plus实现导入excel,并且将数据保存到数据库
vue.js·excel·express
开心点幸运点1 天前
Excel——宏教程(1)
excel
boy快快长大2 天前
将大模型生成数据存入Excel,并用增量的方式存入Excel
java·数据库·excel
Leuanghing2 天前
使用Python生成F分布表并导出为Excel文件
开发语言·python·excel·f分布