基于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>
相关推荐
shouchaobao7 小时前
仓库房进销存Excel模板合集:商品采购+出入库+库存统计一站式管理,适配仓库管理员/中小企业/个体商户
excel
chenhdowue8 小时前
如何使用 vxe-table 导出为带图片的单元格到 excel 格式文件
vue.js·excel·vxe-table·vxe-ui
码上成长8 小时前
从零实现:react&Ts--批量导入 & Excel 模版下载功能
javascript·react.js·excel
前端sweetGirl8 小时前
Excel 里 XLOOKUP 函数返回日期时找不到值显示 1/0,怎么让他不显示
excel
来鸟 鸣间1 天前
excel快速填充
excel
葡萄城技术团队1 天前
Excel 文件到底是怎么坏掉的?深入 OOXML 底层原理讲解修复策略
android·java·excel
程序边界1 天前
AI实战狂飙!Excel图表制作彻底解放双手:从数据清洗到智能预测全攻略
人工智能·excel
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建漏斗图
ui·c#·excel·aspose·excel开发·excel漏斗图·漏斗图
wtsolutions1 天前
WPS另存为JSON,WPS导出JSON, WPS表格转换成JSON : Excel to JSON WPS插件使用指南
json·excel·wps·插件·加载项·wtsolutions
weixin_462446231 天前
使用 Python + Tkinter + openpyxl 实现 Excel 文本化转换
开发语言·python·excel