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、上传效果

 

posted @ 2020-08-04 15:16  幽冥狂_七  阅读(3098)  评论(0编辑  收藏  举报