zoukankan      html  css  js  c++  java
  • Layui upload 上传有进度条

    首先需要下载最新的 layui的js layui 有进度条是在 2.5.5的时候添加了进度条的功能,所有在也不用 xhr:function了

    1、前台HTML

    <link href="~/Scripts/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Scripts/layui/layui.all.js"></script>
     
    <button type="button" class="layui-btn" id="chooseFile">
        <i class="layui-icon">&#xe67c;</i>选择文件
    </button> 文件名称: <span id="fileName"></span>
    <div style="height: 20px;"></div>
    <button type="button" class="layui-btn" id="uploadBtn">
        <i class="layui-icon">&#xe67c;</i>上传文件
    </button>
    <div style="height: 20px;"></div>
    <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
    </div>
    <script type="text/javascript">
    
    
        layui.use(['upload', 'element'], function () {
            var upload = layui.upload,
                element = layui.element;
            element.init();
    
            upload.render({
                elem: '#chooseFile', // 文件选择
                accept: 'file',
                url: '/Home/FileUpload',
                auto: false, // 设置不自动提交
                bindAction: '#uploadBtn', // 提交按钮
                progress: function (e, percent) {
                    console.log(JSON.stringify(e));
                    console.log("进度:" + e + '%');
                    element.progress('progressBar', e + '%');
                },
                choose: function (obj) {
                    obj.preview(function (index, file, result) {
                        $("#fileName").html(file.name);
                    });
                },
                done: function (res) {
                    layer.msg(res.msg);
                },
                error: function (res) {
                    layer.msg(res.msg);
                }
            });
    
        });
    </script>

    2、后台方法

    public ActionResult FileUpload()
            {
                HttpFileCollection uploadFiles = System.Web.HttpContext.Current.Request.Files;
    
                //上传文件保存路径  
                string savePath = Server.MapPath("/UploadFiles/");
                try
                {
                    for (int i = 0; i < uploadFiles.Count; i++)
                    {
    
                        //HttpPostedFile 对已上传文件进行操作
    
                        //uploadFiles[i]  逐个获取上传文件
                        System.Web.HttpPostedFile postedFile = uploadFiles[i];
                      
                        string fileName = System.IO.Path.GetFileName(postedFile.FileName); //获取到名称
                        string fileExtension = System.IO.Path.GetExtension(fileName);//文件的扩展名称
                        string type = fileName.Substring(fileName.LastIndexOf(".") + 1);    //类型  
                        if (uploadFiles[i].ContentLength > 0)
                            if (!System.IO.Directory.Exists(savePath))
                            {
                                System.IO.Directory.CreateDirectory(savePath);
                            }
    
                            uploadFiles[i].SaveAs(savePath + fileName);
                    }
                }
                catch (System.Exception Ex)
                {
                    Response.Write(Ex);
                }
    
                return Content("");
            }

    3、上传效果

  • 相关阅读:
    在Java中使用 break/continue 语句来控制多重嵌套循环的跳转
    Android系统架构基本模式解析
    添加蓝牙通讯功能
    wince串口蓝牙
    Android各层推荐开发书籍及参考资料
    CDEFINES的用法
    蓝牙地址的规则
    WinCE中sources文件中targetlibs与sourcelibs的作用与区别
    Java初学者不得不知的概念,JDK,JRE,JVM的区别?
    WINCE的内存配置config.bib文件的解析
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/13433465.html
Copyright © 2011-2022 走看看