zoukankan      html  css  js  c++  java
  • bootstrap fileinput 多文件上传并回显

    网上很多文章,但是回显出问题,上传有两种格式,一种是异步,一种是同步,我用异步多文件上传时,异步的请求到后台会多次调用上传方法,就是有几个文件就调用几次,所以我用同步方式,一并处理,回调一次

    前端页面

    <head>
        <meta name="viewport" content="width=device-width" />
        <title>日订单导入</title>
        <script src="~/Scripts/fileinput.js"></script>
        <script src="~/Scripts/zh.js"></script>
        <link href="~/Content/fileinput.css" rel="stylesheet" />
    </head>
    <body>
        <div style="text-align:center">
            <form>
                <div class="modal-title">
                    <h4>请选择日订单文件</h4>
                </div>
                <div class="modal-body">
                    <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" />
                </div>
                <button type="submit" class="btn btn-default">提交</button>
            </form>
            </div>
        
    </body>
    </html>
    
    
    <script type="text/javascript">
        $(function () {
            var control = $("#txt_file");
            var uploadrul = "/VW_Import/DayFileUpload";
            control.fileinput({
                language: 'zh', //设置语言
                uploadUrl: uploadrul, //上传的地址
                allowedFileExtensions: ['xls','xlsx'],//接收的文件后缀
                showUpload: true, //显示批量上传按钮
                showCaption: false,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                dropZoneEnabled: true,//是否显示拖拽区域
                uploadAsync: false, //默认异步上传
                showRemove: true, //显示移除按钮
                //showPreview: true, //是否显示预览
                showCaption: false,//是否显示标题
                maxFileCount: 100,
                enctype: 'multipart/form-data',
                validateInitialCount: true,
                previewFileIcon: "<i class='glyphicon glyphicon-file'></i>",
    
                msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
            });
            //导入文件上传完成之后的事件
            $("#txt_file").on("filebatchuploadsuccess", function (event, data, previewId, index) {
                
                result = $.parseJSON(data.response);
                if (result.success == true) {
                    alert("导入成功!");
                }
                else {
                    alert(result.errorMsg);
                }
            });
        });
    </script>

    后台代码

    public ActionResult DayFileUpload()
            {
                try
                {
                    HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                    List<VW_DayParts> lst = new List<VW_DayParts>();
                    List<VW_DayPartsVersion> versionlst = new List<VW_DayPartsVersion>();
                    var checkList = dbVW_DayPartsVersion.Entities().ToList();
                    bool update = true;
    
                    for (int j = 0; j < files.Count; j++)
                    {
                        HttpPostedFile PostedFile = files[j];
                        string fileName = PostedFile.FileName;
    
                        //VW_DayPartsVersion s = checkList.Where(m => m.FileName.Contains(fileName)).FirstOrDefault();
                        if (checkList.Where(m => m.FileName.Contains(fileName)).FirstOrDefault() != null)
                        {
                            update = false;
                            break;
                        }
    
                        string filePath = Server.MapPath("~/VW_Upload/DayOrder/" + fileName);
                        PostedFile.SaveAs(filePath);
    
                        IWorkbook workbook;
                        //XSSFWorkbook hssfworkbook;
                        string fileExt = Path.GetExtension(fileName).ToLower();
                        using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                        {
                            if (fileExt == ".xlsx")
                            {
                                workbook = new XSSFWorkbook(file);
                            }
                            else if (fileExt == ".xls")
                            {
                                workbook = new HSSFWorkbook(file);
                            } else
                            {
                                workbook = null;
                            }
    
                            //workbook = new XSSFWorkbook(file);
    
                        }
    
                        NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(1);
                        //列数
                        int cellCount = sheet.GetRow(0).LastCellNum;
                        //int cellCount = sheet.GetRow(0).LastCellNum;
    
                        UserLoginInfo loginInfo = aService.GetUserLoginInfo();
    
                        VW_DayPartsVersion dVersion = new VW_DayPartsVersion();
                        dVersion.FileName = fileName;
                        dVersion.UpTime = DateTime.Now;
                        dVersion.UserId = loginInfo.UserId;
    
                        string[] tempName = fileName.Split(' ');
                        dVersion.Factory = tempName[1].Substring(0, 4);
                        versionlst.Add(dVersion);
    
                        for (int i = 1; i <= sheet.LastRowNum; i++)
                        {
                            //int j = 0;//0 3 4 17 18
                            VW_DayParts item = new VW_DayParts();
                            IRow currentRow = sheet.GetRow(i);// i=1 从第二行开始
    
                            //物料号
                            if (!string.IsNullOrWhiteSpace(currentRow.GetCell(0).ToString()))
                            {
                                item.MATNR = currentRow.GetCell(0).ToString();
                            }
                            //工厂代码
                            if (!string.IsNullOrWhiteSpace(currentRow.GetCell(3).ToString()))
                            {
                                item.Factory = currentRow.GetCell(3).ToString();
                            }
                            //合同编号
                            if (!string.IsNullOrWhiteSpace(currentRow.GetCell(4).ToString()))
                            {
                                item.ContractNo = currentRow.GetCell(4).ToString();
                            }
                            //交货时间
                            if (!string.IsNullOrWhiteSpace(currentRow.GetCell(17).ToString()))
                            {
                                item.DeliveryDate = Convert.ToDateTime(currentRow.GetCell(17).ToString());
                            }
                            //计划数量
                            if (!string.IsNullOrWhiteSpace(currentRow.GetCell(18).ToString()))
                            {
                                item.Quantity = Convert.ToInt32(currentRow.GetCell(18).NumericCellValue);
                            }
                            item.FileName = fileName;
                            item.UpTime = DateTime.Now;
                            item.UserId = loginInfo.UserId;
                            lst.Add(item);
                        }
                    }
    
                    if(update)
                    {
                        dbVW_DayParts.BulkInsert(lst);
                        dbVW_DayPartsVersion.Insert(versionlst);
                        return Json(MsgCommon.respJsonSuccessMsg());
                    }
                    else
                    {
                        return Json(MsgCommon.respJsonErrorMsg("文件名重复或文件已上传"));
    
                    }
    
                }
                catch (DbEntityValidationException dbEx)
                {            
                    return Json(MsgCommon.respJsonErrorMsg(dbEx.ToString()));
                }
            }
  • 相关阅读:
    Nginx应用详解及配置
    mongodb复制+分片集原理
    memcached架构及缓存策略
    redis数据类型
    redis数据库安装 redis持久化及主从复制
    shell脚本-正则、grep、sed、awk
    kvm虚拟机管理基础
    kvm热添加和热迁移
    zabbix调用api检索方法
    kubernetes deployment升级和回滚
  • 原文地址:https://www.cnblogs.com/seanjack/p/12624335.html
Copyright © 2011-2022 走看看