导入文件数据库存入数据

js

var NotDocs = []; //存放文件路径集合
$(function () {
NotDocs = [];
$(“#file-1”).fileinput(‘destroy’);
initUpload([],[])
}

function initUpload(previewData, previewConfigData) {
$(‘#file-1’).fileinput({
language: ‘zh’, // 多语言设置,需要引入local中相应的js,例如locales/zh.js
theme: “explorer-fa”, // 主题
uploadUrl: setter.host + ‘/Document/BasicInfoUpload?newid=’+‘@ViewBag.NewId’, // 上传地址
validateInitialCount: true,
maxFileCount: 20, // 最大上传数量
overwriteInitial: false, // 覆盖初始预览内容和标题设置
showCancel: false, // 显示取消按钮
showZoom: false, // 显示预览按钮
showCaption: false, // 显示文件文本框
dropZoneEnabled: false, // 是否可拖拽
showRemove: false, // 显示移除按钮
showClose: false,
allowedFileExtensions: [“xls”, “xlsx”,“xlsb”],
fileActionSettings: { // 在预览窗口中为新选择的文件缩略图设置文件操作的对象配置
showRemove: true, // 显示删除按钮
showUpload: true, // 显示上传按钮
showDownload: true, // 显示下载按钮
showZoom: true, // 显示预览按钮
showDrag: false, // 显示拖拽
removeIcon: ‘’, // 删除图标
uploadIcon: ‘’, // 上传图标
uploadRetryIcon: ‘’ // 重试图标
},
initialPreview: previewData,
initialPreviewConfig: previewConfigData,
}).on(‘filedeleted’, function (event, key, jqXHR, data) {
if (jqXHR.responseJSON.code == 0) {
for (var i in NotDocs) {
if (NotDocs[i].path == key) {
NotDocs.splice(i, 1);
}
}
}
});
}
$(‘#file-1’).on(‘fileuploaded’, function (event, data, previewId, index) {
if (data.response.code == 0) {
tipsForm(‘success’, data.response.Message);
NotDocs.push({ name: data.response.name, path: data.response.path });
document.getElementById(“dqjd”).innerText = data.response.jdmsg;
} else {
tipsForm(‘error’, data.response.Message || data.response.msg);
document.getElementById(previewId).getElementsByTagName(‘div’)[8].className += " error";
document.getElementById(previewId).getElementsByTagName(‘div’)[8].innerHTML = “上传失败”;

    }
})

.--------------------------------------
/Document/BasicInfoUpload
public JsonResult BasicInfoUpload()
{
Controller cxt = this;
var newid = cxt.HttpContext.Request.QueryString[“newid”];
try
{
string imgPath = “/Uploads/Images/”;
string docPath = “/Uploads/Document/”;
string videoPath = “/Uploads/Video/”;
string otherPath = “/Uploads/Other/”;
string[] imgArr = new string[] { “.gif”, “.jpg”, “.jpeg”, “.png”, “.bmp” };
string[] docArr = new string[] { “.doc”, “.docx”, “.xls”, “.xlsx”, “.xlsb”, “.pdf”, “.ppt”, “.txt” };
string[] videoArr = new string[] { “.avi”, “.mov”, “.rmvb”, “.rm”, “.flv”, “.mp4”, “.flash” };
string[] otherArr = new string[] { “.zip”, “.rar”, “.7z” };
HttpPostedFileBase uploadFile = null;
//string value1 = cxt.Request[“path”];
//value1 = HttpUtility.UrlDecode(value1);
string path = “”;
string resPath = “”;
uploadFile = cxt.Request.Files[0];
string extName = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf(‘.’));
if (imgArr.Contains(extName))
{
path = Server.MapPath(imgPath);
resPath = imgPath;
}
else if (docArr.Contains(extName))
{
path = Server.MapPath(docPath);
resPath = docPath;
}
else if (videoArr.Contains(extName))
{
path = Server.MapPath(videoPath);
resPath = videoPath;
}
else
{
return Json(new { code = 1, Message = “该文件格式不允许上传!” });
//path = Server.MapPath(otherPath);
//resPath = otherPath;
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string oldName = uploadFile.FileName;
string fileName = Guid.NewGuid().ToString() + uploadFile.FileName.Substring((uploadFile.FileName.LastIndexOf(‘.’)), (uploadFile.FileName.Length - uploadFile.FileName.LastIndexOf(‘.’)));
uploadFile.SaveAs(path + fileName);
Workbook workbook = new Workbook();
workbook.Open(path + fileName);
Cells cells = workbook.Worksheets[0].Cells;
var colCount = cells.MaxColumn + 1;//获取到的列数少一列
//var dataTable1 = cells.ExportDataTable(1, 0, cells.MaxDataRow, colCount);//noneTitle
var dataTable2 = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, colCount, true);//showTitle
//ExportDataTableAsString防止日期类型的字段被读成了数字字符串 例如“4859.321”
sjclProvider provider = new sjclProvider();
TData obj = provider.Save(dataTable2, newid);
return Json(new { code = 0, Message = “上传成功!”, path = resPath + fileName, name = oldName,jdmsg=obj.data });
}
catch (Exception ex)
{
return Json(new { code = 1, Message = ex.Message });
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值