diff --git a/Infrastructure/Enums/StoreType.cs b/Infrastructure/Enums/StoreType.cs
index 4dea7eb..79d8e6d 100644
--- a/Infrastructure/Enums/StoreType.cs
+++ b/Infrastructure/Enums/StoreType.cs
@@ -1,9 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.ComponentModel;
namespace Infrastructure.Enums
{
@@ -30,5 +25,10 @@ namespace Infrastructure.Enums
[Description("腾讯云")]
TENCENT = 3,
+ ///
+ /// 七牛
+ ///
+ [Description("七牛云")]
+ QINIU = 4
}
}
diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs
index d36dff1..1b504b5 100644
--- a/ZR.Admin.WebApi/Controllers/CommonController.cs
+++ b/ZR.Admin.WebApi/Controllers/CommonController.cs
@@ -1,5 +1,6 @@
using Infrastructure;
using Infrastructure.Attribute;
+using Infrastructure.Enums;
using Infrastructure.Extensions;
using Infrastructure.Model;
using Microsoft.AspNetCore.Hosting;
@@ -91,19 +92,53 @@ namespace ZR.Admin.WebApi.Controllers
///
/// 存储目录
/// 自定义文件名
- /// 上传类型 1、发送邮件
+ /// 上传类型1、保存到本地 2、保存到阿里云
///
[HttpPost()]
[Verify]
[ActionPermissionFilter(Permission = "common")]
- public async Task UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
+ public async Task UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", StoreType storeType = StoreType.LOCAL)
{
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
+ SysFile file = new();
+ string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
+ double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
+ string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
+ int MaxContentLength = 15;
+ if (NotAllowedFileExtensions.Contains(fileExt))
+ {
+ return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
+ }
+ switch (storeType)
+ {
+ case StoreType.LOCAL:
+ file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
- SysFile file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
+ break;
+ case StoreType.ALIYUN:
+ if ((fileSize / 1024) > MaxContentLength)
+ {
+ return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
+ }
+ file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, HttpContext.GetName())
+ {
+ StoreType = (int)StoreType.ALIYUN,
+ FileType = formFile.ContentType
+ };
+ file = await SysFileService.SaveFileToAliyun(file, formFile);
+
+ if (file.Id <= 0) { return ToResponse(ApiResult.Error("阿里云连接失败")); }
+ break;
+ case StoreType.TENCENT:
+ break;
+ case StoreType.QINIU:
+ break;
+ default:
+ break;
+ }
return SUCCESS(new
{
- url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
+ url = file.AccessUrl,
fileName,
fileId = file.Id.ToString()
});
@@ -134,7 +169,7 @@ namespace ZR.Admin.WebApi.Controllers
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
}
- SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, "", HttpContext.GetName())
+ SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, HttpContext.GetName())
{
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
FileType = formFile.ContentType
diff --git a/ZR.CodeGenerator/CodeGenerateTemplate.cs b/ZR.CodeGenerator/CodeGenerateTemplate.cs
index 03fcdbc..4e526c2 100644
--- a/ZR.CodeGenerator/CodeGenerateTemplate.cs
+++ b/ZR.CodeGenerator/CodeGenerateTemplate.cs
@@ -89,7 +89,7 @@ namespace ZR.CodeGenerator
//图片
sb.AppendLine(" ");
sb.AppendLine($" ");
- sb.AppendLine($@" ");
+ sb.AppendLine($@" ");
sb.AppendLine(" ");
sb.AppendLine(" ");
}
diff --git a/ZR.Common/AliyunOssHelper.cs b/ZR.Common/AliyunOssHelper.cs
index 4c9bf97..b45c87a 100644
--- a/ZR.Common/AliyunOssHelper.cs
+++ b/ZR.Common/AliyunOssHelper.cs
@@ -43,5 +43,25 @@ namespace ZR.Common
return System.Net.HttpStatusCode.BadRequest;
}
+ ///
+ /// 删除资源
+ ///
+ ///
+ ///
+ ///
+ public static System.Net.HttpStatusCode DeleteFile(string dirPath, string bucketName = "")
+ {
+ if (string.IsNullOrEmpty(bucketName)) { bucketName = bucketName1; }
+ try
+ {
+ OssClient client = new(endpoint, accessKeyId, accessKeySecret);
+ DeleteObjectResult putObjectResult = client.DeleteObject(bucketName, dirPath);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ return System.Net.HttpStatusCode.BadRequest;
+ }
}
}
diff --git a/ZR.Model/System/SysFile.cs b/ZR.Model/System/SysFile.cs
index 501b9c0..81c75be 100644
--- a/ZR.Model/System/SysFile.cs
+++ b/ZR.Model/System/SysFile.cs
@@ -72,14 +72,13 @@ namespace ZR.Model.System
public string AccessUrl { get; set; }
public SysFile() { }
- public SysFile(string originFileName, string fileName, string ext, string fileSize, string storePath, string accessUrl,string create_by)
+ public SysFile(string originFileName, string fileName, string ext, string fileSize, string storePath, string create_by)
{
StorePath = storePath;
RealName = originFileName;
FileName = fileName;
FileExt = ext;
FileSize = fileSize;
- AccessUrl = accessUrl;
Create_by = create_by;
Create_time = DateTime.Now;
}
diff --git a/ZR.Service/System/SysFileService.cs b/ZR.Service/System/SysFileService.cs
index 415a21e..a774d7d 100644
--- a/ZR.Service/System/SysFileService.cs
+++ b/ZR.Service/System/SysFileService.cs
@@ -51,14 +51,15 @@ namespace ZR.Service.System
using (var stream = new FileStream(finalFilePath, FileMode.Create))
{
- await formFile.CopyToAsync(stream);
+ await formFile.CopyToAsync(stream);//await 不能少
}
string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
- SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, userName)
+ SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, userName)
{
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
FileType = formFile.ContentType,
- FileUrl = finalFilePath
+ FileUrl = finalFilePath,
+ AccessUrl = accessPath
};
file.Id = await InsertFile(file);
return file;
diff --git a/ZR.Vue/src/components/FileUpload/index.vue b/ZR.Vue/src/components/FileUpload/index.vue
index 14d85de..a8a9eaf 100644
--- a/ZR.Vue/src/components/FileUpload/index.vue
+++ b/ZR.Vue/src/components/FileUpload/index.vue
@@ -52,7 +52,7 @@ export default {
// 文件类型, 例如['png', 'jpg', 'jpeg']
fileType: {
type: Array,
- default: () => ['doc', 'xls', 'ppt', 'txt', 'pdf', 'svga', 'json']
+ default: () => ['doc', 'xls', 'ppt', 'txt', 'pdf', 'json']
},
// 是否显示提示
isShowTip: {
diff --git a/ZR.Vue/src/components/UploadImage/index.vue b/ZR.Vue/src/components/UploadImage/index.vue
index 8bf4f22..3317d27 100644
--- a/ZR.Vue/src/components/UploadImage/index.vue
+++ b/ZR.Vue/src/components/UploadImage/index.vue
@@ -1,7 +1,7 @@
@@ -20,7 +20,7 @@