多文件(图片,音频,文档等)上传顺序问题(简单记录)

使用背景:

  测试layui的多图片上传中,发现了一个问题。就是说当多图片上传后顺序无法进行更改,在不修改layui的框架前提下这里有两种方案:1,按照上传时间排序,2用js可以滑块进行拖拽修改。

都不难,这里就简单记录一下,话不多说,开撸!(ps:貌似同步上传可以避免这种问题,没有具体研究。)

c#按顺序获取文件夹中的文件

  上传则不变,只需要将获取文件的方法重写一下。代码如图:

复制代码
    public class FileComparer : IComparer
    {
        /// <summary>
        /// 文件排序
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        int IComparer.Compare(object o1, object o2)
        {
            FileInfo fi1 = o1 as FileInfo;
            FileInfo fi2 = o2 as FileInfo;
            return fi1.CreationTime.CompareTo(fi2.CreationTime);
        }
    }
复制代码
复制代码
        public static FileInfo[] GetFilesList(string path)
        {

            var di = new DirectoryInfo(path);//文件夹所在目录
            var fc = new FileComparer();

            FileInfo[] fileList = di.GetFiles();
            Array.Sort(fileList, fc);//按文件创建时间排正序


            return fileList;


        }
复制代码

目前是按照时间排序,可更改为大小,类型,等具体看需求。

 

js滑块的运用

  此方式,是利用了文件名称排序的规则,则利用拖拽修改顺序后,进行重新命名。所使用的ui框架是:https://jqueryui.com/sortable/#display-grid,效果如下:

 

 

 

 

 

代码也很简单直接源代码就可以运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE>
<html lang="zh-cn">
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>拖拽排序</title>
    <style>
        .sortable-ghost {
            opacity: 0.4;
            background-color: #F4E2C9;
        }
 
        .block__list li {
            cursor: pointer;
 
        }
    </style>
</head>
<!--      <link href="app.css" rel="stylesheet" type="text/css"/>
      -->
<!-- script src="./Sortable.js"></script> -->
<script src="https://cdn.bootcss.com/Sortable/1.8.3/Sortable.js"></script>
 
<body>
    <button id="btn">关闭拖拽</button>
    <ul id="foo" class="block__list block__list_words">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
    </ul>
    <script>
       var sortable = new Sortable(document.getElementById('foo'), {
            animation: 150, //动画参数
            disabled: false,//开关状态
            onAdd: function (evt) {   //拖拽时候添加有新的节点的时候发生该事件
                console.log('onAdd.foo:', [evt.item, evt.from]);
            },
            onUpdate: function (evt) {  //拖拽更新节点位置发生该事件
                console.log('onUpdate.foo:', [evt.item, evt.from]);
            },
            onRemove: function (evt) {   //删除拖拽节点的时候促发该事件
                console.log('onRemove.foo:', [evt.item, evt.from]);
            },
            onStart: function (evt) {  //开始拖拽出发该函数
                console.log('onStart.foo:', [evt.item, evt.from]);
            },
            onSort: function (evt) {  //发生排序发生该事件
                console.log('onSort.foo:', [evt.item, evt.from]);
            },
            onEnd: function (evt) { //拖拽完毕之后发生该事件
                console.log('onEnd.foo:', [evt.item, evt.from]);
            }
        });
        var btn = document.getElementById('btn')
        btn.onclick = function () {
            console.log('123')
            console.log(sortable)
            var state = sortable.option("disabled"); // get
            console.log(state)
            sortable.option("disabled", !state); // set
        }
    </script>
</body>
 
</html>

  

 

posted @   黄金程序员  阅读(1400)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示