Skip to content

Commit

Permalink
optimize file display
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchunfu committed Nov 2, 2021
1 parent 2b31f99 commit 69d7d66
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 60 deletions.
45 changes: 36 additions & 9 deletions api/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/wuchunfu/fileserver/middleware/configx"
"github.com/wuchunfu/fileserver/middleware/logx"
"github.com/wuchunfu/fileserver/utils/bytex"
"github.com/wuchunfu/fileserver/utils/datetimex"
"io/fs"
"net/http"
"os"
"path/filepath"
)

type FileList struct {
BasePath string `json:"basePath"`
FilePath string `json:"filePath"`
FileName string `json:"fileName"`
FileType string `json:"fileType"`
FileSize string `json:"fileSize"`
DateTime string `json:"dateTime"`
}
Expand All @@ -22,23 +26,46 @@ func List(ctx *gin.Context) {
storagePath := setting.System.StoragePath

fileList := make([]FileList, 0)
storageAbs, _ := filepath.Abs(storagePath)
storageAbsPath, _ := filepath.Abs(storagePath)
// 遍历目录,读出文件名、大小
filepath.Walk(storageAbs, func(filePath string, fileInfo os.FileInfo, err error) error {
err := filepath.WalkDir(storageAbsPath, func(filePath string, info fs.DirEntry, err error) error {
fileInfo, err := info.Info()
if nil == fileInfo {
return err
}
if fileInfo.IsDir() {
if filePath == storageAbsPath {
return nil
}
list := &FileList{
FileName: fileInfo.Name(),
FileSize: bytex.FormatFileSize(fileInfo.Size()),
DateTime: datetimex.FormatDateTime(fileInfo.ModTime()),
list := &FileList{}
if info.IsDir() {
list = &FileList{
BasePath: storageAbsPath,
FilePath: filePath,
FileName: info.Name(),
FileType: "dir",
FileSize: "-",
DateTime: datetimex.FormatDateTime(fileInfo.ModTime()),
}
fileList = append(fileList, *list)
return filepath.SkipDir
}
if filePath != storageAbsPath {
list = &FileList{
BasePath: storageAbsPath,
FilePath: filePath,
FileName: info.Name(),
FileType: "file",
FileSize: bytex.FormatFileSize(fileInfo.Size()),
DateTime: datetimex.FormatDateTime(fileInfo.ModTime()),
}
fileList = append(fileList, *list)
}
fileList = append(fileList, *list)
return nil
})
if err != nil {
logx.GetLogger().Sugar().Errorf("Traversal file directory failed!\n%s", err.Error())
panic(err.Error())
}
// 返回目录json数据
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
Expand Down
55 changes: 4 additions & 51 deletions web/src/views/home/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
@selection-change="handleSelectionChange"
>
<el-table-column align="center" type="selection" width="50px"></el-table-column>
<el-table-column align="center" prop="fileName" label="名称"></el-table-column>
<el-table-column align="center" prop="fileSize" label="大小"></el-table-column>
<el-table-column align="center" prop="dateTime" label="上传时间"></el-table-column>
<el-table-column align="center" prop="fileName" label="文件名称"></el-table-column>
<el-table-column align="center" prop="fileType" label="文件类型"></el-table-column>
<el-table-column align="center" prop="fileSize" label="文件大小"></el-table-column>
<el-table-column align="center" prop="dateTime" label="修改时间"></el-table-column>
<el-table-column align="center" width="100px" label="操作">
<template v-slot="scope">
<el-button
Expand Down Expand Up @@ -260,54 +261,6 @@ export default defineComponent({
});
};
// const handleDownload = (index: number, rows: any) => {
// const fileName = rows.fileName;
// let config: AxiosRequestConfig = {
// headers: {
// 'Accept': 'application/json',
// 'Content-Type': 'application/json; charset=utf-8',
// 'withCredentials': 'true'
// },
// // 表明返回服务器返回的数据类型
// // 表示接收的数据为二进制文件流
// responseType: 'blob',
// }
// getData(`/download/${ fileName }`, config).then((res: any) => {
// // console.log(res);
// const blob = new Blob([res.data], { type: 'application/octet-stream' })
// if (typeof window.navigator.msSaveBlob !== 'undefined') {
// // 兼容IE10+,window.navigator.msSaveBlob:以本地方式保存文件
// window.navigator.msSaveBlob(blob, decodeURI(fileName));
// } else {
// // 在拿到数据流之后,把流转为指定文件格式并创建a标签,模拟点击下载,实现文件下载功能
// // 通过 FileReader 接受并解析, 读取文件
// let reader = new FileReader();
// // 把读取的Blob和File对象以data:URL的形式返回,它与readAsArrayBuffer方法相同
// reader.readAsDataURL(blob);
// // 加载监听
// reader.onloadend = (e) => {
// let link = document.createElement('a');
// link.style.display = 'none';
// link.href = e.target.result;
// link.setAttribute("download", decodeURI(fileName));
// // 兼容:某些浏览器不支持HTML5的download属性
// if (typeof link.download === 'undefined') {
// link.setAttribute('target', '_blank');
// }
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// }
// }
// }).catch((res: any) => {
// console.log(res);
// ElMessage({
// type: 'error',
// message: '下载失败!'
// });
// });
// };
const handleBatchDelete = () => {
const fileNameList = state.multipleSelection
const params = {
Expand Down

0 comments on commit 69d7d66

Please sign in to comment.