Example #1
0
        private static SharedResultVM UploadCheck(IFormFile file, byte[] content, string ext, string subdir)
        {
            var vm = new SharedResultVM();

            if (file != null)
            {
                ext = Path.GetExtension(file.FileName);
            }

            if (string.IsNullOrWhiteSpace(ext) || !ext.Contains('.') || ext.EndsWith("exe"))
            {
                vm.Set(SharedEnum.RTag.refuse);
                vm.Msg = "Invalid extension";
            }
            else
            {
                var    now      = DateTime.Now;
                string filename = now.ToString("HHmmss") + RandomTo.NumCode() + ext;

                if (!string.IsNullOrWhiteSpace(subdir) && !ParsingTo.IsLinkPath(subdir))
                {
                    vm.Set(SharedEnum.RTag.invalid);
                    vm.Msg = "subdir 仅为字母、数字";
                }
                else
                {
                    //虚拟路径
                    var vpath = PathTo.Combine(subdir, now.ToString("yyyy'/'MM'/'dd"));
                    //物理根路径
                    var prp = GlobalTo.GetValue("StaticResource:PhysicalRootPath").Replace("~", GlobalTo.ContentRootPath);
                    //物理路径
                    var ppath = PathTo.Combine(prp, vpath);
                    //创建物理目录
                    if (!Directory.Exists(ppath))
                    {
                        Directory.CreateDirectory(ppath);
                    }

                    using var fs = new FileStream(PathTo.Combine(ppath, filename), FileMode.CreateNew);
                    if (file != null)
                    {
                        file.CopyTo(fs);
                    }
                    else
                    {
                        fs.Write(content, 0, content.Length);
                    }
                    fs.Flush();
                    fs.Close();

                    //输出
                    vm.Data = new
                    {
                        server = GlobalTo.GetValue("StaticResource:Server"),
                        path   = PathTo.Combine(vpath, filename)
                    };
                    vm.Set(SharedEnum.RTag.success);
                }
            }

            return(vm);
        }