/// <summary> /// 上传文件的具体实现 /// </summary> /// <param name="input"></param> /// <returns></returns> public virtual async Task <UploadOutput> UploadCoreAsync(UploadCoreInput input) { var output = new UploadOutput(); try { //文件保存的绝对路径 var absolutePath = Path.Combine($"{ServiceConfig.WebRootPath}\\", $"{input.UrlPrefix}{input.FileName}"); if (!Directory.Exists(Path.GetDirectoryName(absolutePath))) { Directory.CreateDirectory(Path.GetDirectoryName(absolutePath)); } File.WriteAllBytes(absolutePath, input.FileBytes); output.State = "SUCCESS"; output.Original = input.OriginalFileName; output.Url = Path.GetRelativePath(Path.Combine($"{ServiceConfig.WebRootPath}\\", input.UrlPrefix), absolutePath).Replace("\\", "/"); } catch (Exception ex) { throw new UEditorServiceException("文件访问错误", ex.Message); } return(await Task.FromResult(output)); }
/// <summary> /// 封装上传文件需要的参数 /// </summary> /// <returns></returns> public async Task <UploadCoreInput> GetUploadParamAsync() { UploadCoreInput input = new UploadCoreInput(); var uploadFieldName = ""; var allowExtensionList = new List <string>(); var allowSize = 0; var format = ""; var urlPrefix = ""; //涂鸦和截图均为Base64数据上传 var isBase64Content = "uploadscrawl".Equals(Action); if (isBase64Content) { input.FileName = "scrawl.png"; input.FileBytes = Convert.FromBase64String(Context.Request.Form[UEditorConfig.ScrawlFieldName]); if (input.FileBytes.Length > UEditorConfig.ScrawlMaxSize) { throw new UEditorServiceException($"文件大小超出限制"); } format = UEditorConfig.ScrawlPathFormat; urlPrefix = UEditorConfig.ScrawlUrlPrefix; } else { //取得公共参数 switch (Action) { case "uploadimage": uploadFieldName = UEditorConfig.ImageFieldName; allowExtensionList = UEditorConfig.ImageAllowFiles; allowSize = UEditorConfig.ImageMaxSize; format = UEditorConfig.ImagePathFormat; urlPrefix = UEditorConfig.ImageUrlPrefix; break; case "uploadvideo": uploadFieldName = UEditorConfig.VideoFieldName; allowExtensionList = UEditorConfig.VideoAllowFiles; allowSize = UEditorConfig.VideoMaxSize; format = UEditorConfig.VideoPathFormat; urlPrefix = UEditorConfig.VideoUrlPrefix; break; case "uploadfile": uploadFieldName = UEditorConfig.FileFieldName; allowExtensionList = UEditorConfig.FileAllowFiles; allowSize = UEditorConfig.FileMaxSize; format = UEditorConfig.FilePathFormat; urlPrefix = UEditorConfig.FileUrlPrefix; break; } var file = Context.Request.Form.Files[uploadFieldName]; input.OriginalFileName = file.FileName; input.FileName = file.FileName; if (!allowExtensionList.Select(p => p.ToLower()).Contains(Path.GetExtension(file.FileName))) { throw new UEditorServiceException($"文件格式被禁止上传"); } if (file.Length > allowSize) { throw new UEditorServiceException($"文件大小超出限制"); } try { var fileBytes = new byte[file.Length]; await file.OpenReadStream().ReadAsync(fileBytes, 0, fileBytes.Length); input.FileBytes = fileBytes; } catch (Exception ex) { throw new UEditorServiceException($"网络错误", ex.Message); } } input.UrlPrefix = urlPrefix; input.FileName = UEditorPathFormat(input.FileName, format); //处理上传路径,移除路径前的“/”以免造成后续路径拼接出错 if (input.UrlPrefix.StartsWith("/")) { input.UrlPrefix = input.UrlPrefix.Substring(1, input.UrlPrefix.Length - 1); } //在路径结尾添加“/”以免造成后续路径拼接出错 if (input.UrlPrefix.Length > 1 && !input.UrlPrefix.EndsWith("/")) { input.UrlPrefix += "/"; } return(await Task.FromResult(input)); }