基于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>
相关推荐
Non-existent98713 天前
WPS批量清理单元格空白字符的4种方法-异常数字格式处理-实战
excel·wps
Channing Lewis13 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
jarreyer13 天前
【数据分析绘图】excel绘图和bi工具区别
数据挖掘·数据分析·excel
chatexcel13 天前
ChatExcel Max使用教程:图片、PDF、网页与复杂Excel的一站式数据分析
数据分析·pdf·excel
cngkqy13 天前
excel从某一列中用match筛选匹配的数据
excel
qq_5469372713 天前
Excel批量转PDF_Word_图片,支持自动合并报表,效率翻倍。
pdf·word·excel
ai_coder_ai13 天前
在自动化脚本中操作excel文件
运维·自动化·excel
三千花灯13 天前
【Playwright】 自动化测试之参数化登录(Excel/CSV 数据源)
人工智能·机器学习·excel
罗政13 天前
AI工作流实现Excel全自动化(支持SQL)-案例:医院门诊排班表
人工智能·自动化·excel
小妖66613 天前
excel 怎么在单元格内容自动加上一段文字不能用公式
excel·vba