基于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>
相关推荐
Smilecoc13 小时前
excel单、双字节字符转换函数(中英文输入法符号转换)
excel
Terry_trans13 小时前
工作中遇到的EXCEL小问题:多行有间隔符的合并
excel
舊時王謝堂前燕13 小时前
Vue2+Element实现Excel文件上传下载预览【超详细图解】
excel
如意机反光镜裸15 小时前
一键导出数据库表到Excel
数据库·excel·导出
XMYX-017 小时前
用Python3脚本实现Excel数据到TXT文件的智能转换:自动化办公新姿势
python·自动化·excel
graphyer18 小时前
Google sheet 复制excel内容自动合并单元格问题
excel·google sheet
如意机反光镜裸18 小时前
批量导出数据库表到Excel
数据库·excel·批量导出
老胖闲聊1 天前
Python在实际工作中的运用-通用格式CSV文件自动转换XLSX
python·excel
运维技术小记1 天前
升级Office软件后,Windows 系统右键里没有新建Word、Excel、PowerPoint文件的解决办法
windows·word·excel
Stestack1 天前
Python 给 Excel 写入数据的四种方法
前端·python·excel