基于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>
相关推荐
神奇夜光杯4 小时前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
小c君tt5 小时前
MFC中Excel的导入以及使用步骤
c++·excel·mfc
一名技术极客8 小时前
Vue2 doc、excel、pdf、ppt、txt、图片以及视频等在线预览
pdf·powerpoint·excel·文件在线预览
用余生去守护8 小时前
【反射率】-- Lab 转换(excel)
excel
进击的六角龙8 小时前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel
TracyDemo8 小时前
excel功能
excel
lc寒曦8 小时前
【VBA实战】用Excel制作排序算法动画
排序算法·excel·vba
zzzgd8168 小时前
easyexcel实现自定义的策略类, 最后追加错误提示列, 自适应列宽,自动合并重复单元格, 美化表头
java·excel·表格·easyexcel·导入导出
努力学习技能的LY8 小时前
Excel:vba实现批量插入图片批注
excel
图片转成excel表格9 小时前
wps怎么算出一行1和0两种数值中连续数值1的个数,出现0后不再计算?
excel·wps