Esempio n. 1
0
        public WebUploadInfo(IFormFile file)
        {
            TkDebug.ThrowIfNoAppSetting();

            FileSize    = (int)file.Length;
            ContentType = file.ContentType;
            FileName    = Path.GetFileName(file.FileName);
            WebAppSetting appsetting  = WebAppSetting.WebCurrent;
            string        newFileName = Guid.NewGuid() + Path.GetExtension(FileName);

            ServerPath = Path.Combine(appsetting.UploadTempPath, newFileName);
            string urlPath = UriUtil.TextCombine(appsetting.UploadTempVirtualPath, newFileName);

            WebPath = AppUtil.ResolveUrl(urlPath);
        }
Esempio n. 2
0
        public OutputData DoAction(IInputData input)
        {
            try
            {
                TkDebug.Assert(input.IsPost, "调用错误,只能使用Post方式", this);

                HttpRequest request = WebGlobalVariable.Request;
                IFormFile   file    = request.ReadFormAsync().GetAwaiter().GetResult()?.Files["imgFile"];
                if (file == null)
                {
                    throw new ToolkitException("请选择文件。", this);
                }

                String dirName = input.QueryString["dir"];
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = "image";
                }
                var fileExts = EXT_TABLE[dirName];
                if (fileExts == null)
                {
                    throw new ToolkitException("目录名不正确。", this);
                }

                // 检查大小
                int maxLen = input.QueryString["MaxSize"].Value <int>();
                maxLen = maxLen > 0 ? Math.Min(maxLen, MAX_SIZE) : MAX_SIZE;
                if (file.Length > maxLen)
                {
                    throw new ToolkitException("上传文件大小超过限制。", this);
                }

                // 检查扩展名
                string fileExt = Path.GetExtension(file.FileName).ToLower();
                if (string.IsNullOrEmpty(fileExt) || !fileExts.Contains(fileExt.Substring(1)))
                {
                    throw new ToolkitException(string.Format(ObjectUtil.SysCulture,
                                                             "上传文件扩展名是不允许的扩展名。\n只允许{0}格式。", string.Join(",", fileExts)), this);
                }

                // 生成路径
                DateTime now      = DateTime.Now;
                string   ymd      = now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
                string   filePath = input.QueryString["UploadPath"];
                if (string.IsNullOrEmpty(filePath))
                {
                    filePath = fUploadPath;
                }
                else if (!Path.IsPathRooted(filePath))
                {
                    filePath = Path.Combine(WebAppSetting.WebCurrent.SolutionPath, filePath);
                }
                filePath = Path.Combine(filePath, "kindeditor", ymd);
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string fileName = now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                using (FileStream stream = new FileStream(Path.Combine(filePath, fileName),
                                                          FileMode.OpenOrCreate, FileAccess.Write))
                {
                    file.CopyToAsync(stream);
                }

                string urlPath = input.QueryString["VirtualPath"];

                if (string.IsNullOrEmpty(urlPath))
                {
                    urlPath = fVirtualPath;
                }
                bool useRelativePath = input.QueryString["RelativePath"].Value <bool>();

                //if (useRelativePath)
                //    urlPath = VirtualPathUtility.MakeRelative(request.Url.AbsolutePath, urlPath);
                //else
                urlPath = AppUtil.ResolveUrl(urlPath);
                string url = UriUtil.TextCombine(urlPath, string.Format(ObjectUtil.SysCulture,
                                                                        "{0}/{1}/{2}", "kindeditor", ymd, fileName));

                return(OutputData.CreateToolkitObject(new SucessResult(url)));
            }
            catch (Exception ex)
            {
                return(OutputData.CreateToolkitObject(new ErrorResult(ex.Message)));
            }
        }