Beispiel #1
0
        public IActionResult ImportExcel(IFormFile file)
        {
            if (file == null || file.Length <= 0)
            {
                return(BootJsonH((false, PubConst.File1)));
            }
            string fileExt = Path.GetExtension(file.FileName).ToLower();

            if (!NpoiUtil.excel.Contains(fileExt))
            {
                return(BootJsonH((false, PubConst.File2)));
            }
            var filepath = Path.Combine(WebRoot, "upload", PubId.GetUuid()) + fileExt;

            //1 直接通过流
            return(DelegateUtil.TryExecute <IActionResult>(() =>
            {
                using (var st = new MemoryStream())
                {
                    file.CopyTo(st);
                    var dt = NpoiUtil.Import(st, fileExt);
                    var json = _customerServices.Import(dt, UserDtoCache.UserId).JilToJson();
                    return Content(json);
                }
            }, BootJsonH((false, PubConst.File3))
                                                           ));
            //using (var st = new MemoryStream())
            //{
            //    file.CopyTo(st);
            //    var dt = NpoiUtil.Import(st, fileExt);
            //    var json = _customerServices.Import(dt, UserDtoCache.UserId).JilToJson();
            //    return Content(json);
            //}
            //2 先上传到服务器,然后在读取
            //using (var stream = new FileStream(filepath, FileMode.CreateNew))
            //{
            //    file.CopyTo(stream);
            //}
            //var dt = NpoiUtil.Import(filepath);
            //FileUtil.Delete(filepath);
            //var json = _customerServices.Import(dt, UserDtoCache.UserId).JilToJson();
            //return Content(json);
        }
        public IActionResult UploadHeadImg(IFormFile file)
        {
            if (file == null || file.Length <= 0)
            {
                return(BootJsonH((false, PubConst.File1)));
            }
            string fileExt  = Path.GetExtension(file.FileName).ToLower();
            var    img      = Path.Combine("upload", "head", PubId.GetUuid()) + fileExt;
            var    filepath = Path.Combine(WebRoot, img);

            return(DelegateUtil.TryExecute <IActionResult>(() =>
            {
                using (var stream = new FileStream(filepath, FileMode.CreateNew))
                {
                    file.CopyTo(stream);
                }
                var flag = _userServices.Update(new Sys_user {
                    UserId = SqlFunc.ToInt64(UserDtoCache.UserId), HeadImg = img, ModifiedBy = UserDtoCache.UserId, ModifiedDate = DateTimeExt.DateTime
                }, c => new { c.HeadImg, c.ModifiedBy, c.ModifiedDate });
                return BootJsonH(flag ? (flag, PubConst.File6) : (flag, PubConst.File7));
            }, BootJsonH((false, PubConst.File5))
                                                           ));
        }