Example #1
0
        public JsonResult UpLoadKnowledge(string knowledgeFile)
        {
            var        model = new ActionResultModel <string>();
            UploadFile uf    = new UploadFile();

            uf.SetMaxSizeM(1000);               //上传文件大小
            string url        = "/UpLoadFile/"; //文件保存路径
            string saveFolder = Server.MapPath(url);

            uf.SetFileDirectory(saveFolder);
            string         filesName      = System.Web.HttpContext.Current.Request.Params["knowledgeFile"];
            HttpPostedFile file           = System.Web.HttpContext.Current.Request.Files[filesName];
            var            reponseMessage = uf.Save(file, "KnowledgeImport");//保存文件
            UploadHelper   uploadHelper   = new UploadHelper();
            string         readPath       = Server.MapPath("/UpLoadFile/KnowledgeImport/" + reponseMessage.FileName);
            DataTable      dt             = uploadHelper.GetDataByExcel(readPath);

            if (dt == null || dt.Rows.Count == 0 || dt.Rows.Count == 1)
            {
                model.respnseInfo = "2";  //表格为空
            }
            else if (dt.Columns.Count != 2)
            {
                model.respnseInfo = "3";  //模板不对
            }
            else
            {
                string msg = string.Empty;
                model.isSuccess = _draftLogic.InsertExcelToDatabase(dt, ref msg);
                if (msg != string.Empty)
                {
                    model.respnseInfo = msg;
                }
                else
                {
                    model.respnseInfo = model.isSuccess ? "1" : "0";
                }
            }
            System.IO.File.Delete(readPath);
            return(Json(model, JsonRequestBehavior.AllowGet));
        }