分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

上传功能

发布时间:2023-09-06 01:09责任编辑:蔡小小关键词:暂无标签

前台:用Uploader插件写好前台

 var uploader1 = null;
    var uploadExcelUrl = ‘@Url.Action(uploadAction, controllerName)‘;//后台获取前台导入的文件

function InitUploader() {
        uploader1 = $.fn.cfwinUploader.upload({
            //baseUrl: ‘/Scripts/cfw.webuploader/‘,
            postUrl: uploadExcelUrl,
            // 定义按钮
            btnAddFile: $(‘#uploadExcel‘),
            // 设置上传类型,0为图片,1为文件,2为文件和图片
            type: 1,
            // 限制文件个数
            fileNumLimit: 10,
            // 请求参数
            params: { tenatName: ‘‘, blobId: ‘‘ },
            // 文件限制
            configure: {
                fileMaxSize: 15,
                fileExt: ‘xls,xlsx‘,
            },
            // 回调方法
            callback: {
                // 上传过程中触发
                uploadProgress: function (file, percentage) {
                    $("#" + file.id + " .progress-bar").css(‘width‘, (percentage * 100).toFixed(2) + "%");
                },
                uploadComplete: function (file) { //不管成功或者失败,文件上传完成时触发
                },
                uploadSuccess: function (file, data) {
                    uploader1.reset();
                    if (data) {
                        if (data.hasOwnProperty("success")) {
                            if (data.success) {
                                if (data.Result.length > 0) {
                                    data.Result.forEach(function(el, index) {
                                   
                                    });
                                    $.messager.showInfoCenter(‘系统提示‘, ‘上传数据成功!‘);
                                }
                            } else {
                                $.messager.showErrorCenter(‘错误消息‘, data.message);
                            }
                        } else {
                            $.messager.alert(‘系统提示‘, "抱歉,你不具有当前操作的权限!", ‘error‘, function () {
                                //window.location.href = window.location.href;
                            });
                        }
                    } else {
                        $.messager.showErrorCenter(‘错误消息‘, ‘上传数据失败!‘);
                    }
                    $(‘#datagrid‘).datagrid(‘reload‘);
                    $(‘#‘ + file.id).remove();
                },
                uploadError: function (file, reason) {
                    $.messager.showErrorCenter(‘错误消息‘, ‘上传数据失败,失败原因:‘ + reason);
                },
                onFileQueued: function (file) {
                    uploader1.upload();
                }
            }
        });
    }

后台: //导入
        //上传组件
        public JsonResult uploadAction(HttpPostedFileBase file, string detailList = null)
        {
            var json = new JsonSerializer();
            var list = new List<StoreOtherInDetailDTO>();
            if (detailList != null)
            {
                JsonReader reader = new JsonTextReader(new StringReader(detailList));
                list = json.Deserialize<List<StoreOtherInDetailDTO>>(reader);
            }
            return GetServiceJsonResult(() =>
            {
                var result = ScmService.OtherInExcelTemplate(file.InputStream, list);//Service层处理具体代码
                return result;
            });
        }

Service层处理具体代码

 public List<List<RowValue>> GetWorksheetRowListData(int sheetIndex) ???????{ ???????????try ???????????{ ???????????????var sheet = Workbook.GetSheetAt(sheetIndex); ???????????????if (sheet == null) ???????????????????return null; ???????????????var result = new List<List<RowValue>>(); ???????????????var headerRow = sheet.GetRow(0); ???????????????var cellCount = headerRow.LastCellNum; ???????????????var maxRow = sheet.LastRowNum; ???????????????for (var i = 0; i <= maxRow; i++) ???????????????{ ???????????????????var row = sheet.GetRow(i); ???????????????????var emptyRow = true; ???????????????????if (row == null) continue; ???????????????????cellCount = cellCount != 0 ? cellCount : row.LastCellNum; ???????????????????var columns = new List<RowValue>(); ???????????????????for (int j = 0; j < cellCount; j++) ???????????????????{ ???????????????????????var cell = row.GetCell(j); ???????????????????????????????????????????????var alphabet = IndexToColumn(j + 1); ???????????????????????var headerCell = headerRow.GetCell(j); ???????????????????????var headerValue = GetCellValue(headerCell); ???????????????????????var columnName = !string.IsNullOrEmpty(headerValue) ???????????????????????????? headerValue ???????????????????????????: i.ToString(); ???????????????????????var cellValue = string.Empty; ???????????????????????if (cell != null) ???????????????????????{ ???????????????????????????cellValue = GetCellValue(cell); ???????????????????????}; ???????????????????????if (cellValue != null && !string.IsNullOrEmpty(cellValue.Trim())) ???????????????????????{ ???????????????????????????emptyRow = false; ???????????????????????} ???????????????????????var rowValue = new RowValue ???????????????????????{ ???????????????????????????RowId = i, ???????????????????????????ColumnId = j, ???????????????????????????CellName = alphabet, ???????????????????????????ColumnName = columnName, ???????????????????????????CellValue = cellValue ???????????????????????}; ???????????????????????columns.Add(rowValue); ???????????????????} ???????????????????if (!emptyRow) ???????????????????{ ???????????????????????result.Add(columns); ???????????????????} ???????????????} ???????????????return result; ???????????} ???????????catch (Exception ex) ???????????{ ???????????????if (Workbook != null) ???????????????{ ???????????????????Workbook.Close(); ???????????????????Workbook = null; ???????????????} ???????????????LogUtil.LogError("Couldn‘t get the row‘s List<RowValue> data by sheet name.", ex.Message + Environment.NewLine + ex.StackTrace); ???????????????return null; ???????????} ???????}

上传功能

原文地址:http://www.cnblogs.com/BaoWeiHome/p/7493283.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved