1. 程式人生 > >上傳功能

上傳功能

原因 div lln toc fixed max reload ogr i++

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

上傳功能