上传功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传功能相关的知识,希望对你有一定的参考价值。

前台:用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;
            }
        }

 

以上是关于上传功能的主要内容,如果未能解决你的问题,请参考以下文章

IOS开发-OC学习-常用功能代码片段整理

vue2.0 代码功能片段

laravel特殊功能代码片段集合

VIM 代码片段插件 ultisnips 使用教程

sublime代码片段功能

JavaScript实用功能代码片段