Esempio n. 1
0
        public async Task <bool> SubmitForm(UploadfileEntity entity, string keyValue)
        {
            try
            {
                await _service.SubmitForm(entity, keyValue);
                await Success("操作成功。", "", keyValue);

                return(true);
            }
            catch (Exception ex)
            {
                await Error(ex.Message, "", keyValue);

                return(false);
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> Upload(string fileby, int filetype = 0)
        {
            try
            {
                //1图片,2excel
                int[] filetypes = { 1, 2 };
                if (!filetypes.Contains(filetype))
                {
                    throw new Exception("请指定文件格式");
                }
                string stemp = "local";
                if (_service.currentuser.CompanyId != GlobalContext.SystemConfig.SysemMasterProject)
                {
                    var temp = await _setService.GetForm(_service.currentuser.CompanyId);

                    if (temp != null)
                    {
                        stemp = temp.F_CompanyName;
                    }
                    else
                    {
                        throw new Exception("租户不存在");
                    }
                }
                var  files = HttpContext.Request.Form.Files;
                long size  = files.Sum(f => f.Length);
                if (size > 104857600)
                {
                    throw new Exception("大小必须小于100M");
                }
                List <object> list = new List <object>();
                foreach (var file in files)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    var ispic    = FileHelper.IsPicture(fileName);
                    if (filetype == 1 && !ispic)
                    {
                        throw new Exception("请上传图片");
                    }
                    var isexcle = FileHelper.IsExcel(fileName);
                    if (filetype == 2 && !isexcle)
                    {
                        throw new Exception("请上传Excel");
                    }
                    if (ispic)
                    {
                        filetype = 1;
                    }
                    if (isexcle)
                    {
                        filetype = 2;
                    }
                    string fileValue = "";
                    if (fileby == "公司logo")
                    {
                        fileValue = "icon";
                    }
                    else
                    {
                        fileValue = "file";
                    }
                    string filePath = "";
                    fileName = Utils.CreateNo() + fileName.Substring(fileName.LastIndexOf("."));
                    UploadfileEntity entity = new UploadfileEntity();
                    if (!string.IsNullOrEmpty(stemp))
                    {
                        entity.F_FilePath = $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/" + fileName;
                        filePath          = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/";
                    }
                    string fileFullName = filePath + fileName;
                    entity.Create();
                    entity.F_EnabledMark     = true;
                    entity.F_FileBy          = fileby;
                    entity.F_FileType        = filetype;
                    entity.F_CreatorUserName = _service.currentuser.UserName;
                    entity.F_FileSize        = size.ToIntOrNull();

                    entity.F_FileName   = fileName;
                    entity.F_OrganizeId = _service.currentuser.DepartmentId;
                    if (fileName.LastIndexOf(".") >= 0)
                    {
                        entity.F_FileExtension = fileName.Substring(fileName.LastIndexOf("."));
                    }
                    if (!await SubmitForm(entity, ""))
                    {
                        throw new Exception("数据库操作失败");
                    }
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    using (FileStream fs = System.IO.File.Create(fileFullName))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }
                    list.Add(new { src = entity.F_FilePath, title = fileName });
                }
                await _logService.WriteLog("操作成功。", "", "", DbLogType.Visit);

                return(Content(new { code = 0, msg = "操作成功", data = list }.ToJson()));
            }
            catch (Exception ex)
            {
                await _logService.WriteLog(ex.Message, "", "", DbLogType.Visit, true);

                return(Content(new { code = 400, msg = "操作失败," + ex.Message }.ToJson()));
            }
        }