优化本地文件上传
This commit is contained in:
parent
c6acbf407e
commit
c23b69a883
34
Infrastructure/Enums/StoreType.cs
Normal file
34
Infrastructure/Enums/StoreType.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Enums
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件存储位置
|
||||
/// </summary>
|
||||
public enum StoreType
|
||||
{
|
||||
/// <summary>
|
||||
/// 本地
|
||||
/// </summary>
|
||||
[Description("本地")]
|
||||
LOCAL = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 阿里云
|
||||
/// </summary>
|
||||
[Description("阿里云")]
|
||||
ALIYUN = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 腾讯云
|
||||
/// </summary>
|
||||
[Description("腾讯云")]
|
||||
TENCENT = 3,
|
||||
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@ namespace Infrastructure
|
||||
public static string GetdirPath(string path = "")
|
||||
{
|
||||
DateTime date = DateTime.Now;
|
||||
string timeDir = Path.Combine(date.ToString("yyyyMMdd"));// date.ToString("yyyyMM/dd/HH/");
|
||||
string timeDir = date.ToString("yyyyMMdd");// date.ToString("yyyyMM/dd/HH/");
|
||||
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
@ -28,7 +28,7 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 取文件名的MD5值(16位)
|
||||
/// </summary>
|
||||
/// <param name="name">文件名,不包括扩展名</param>
|
||||
/// <param name="str">文件名,不包括扩展名</param>
|
||||
/// <returns></returns>
|
||||
public static string HashFileName(string str = null)
|
||||
{
|
||||
|
||||
@ -110,13 +110,12 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
string hashFileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower();
|
||||
string hashFileName = FileUtil.HashFileName();
|
||||
fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
|
||||
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
|
||||
string filePath = FileUtil.GetdirPath(fileDir);
|
||||
string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, filePath, fileName);
|
||||
finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/");
|
||||
double fileSize = formFile.Length / 1024;
|
||||
double fileSize = formFile.Length / 1024.0;
|
||||
|
||||
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
|
||||
{
|
||||
@ -128,18 +127,10 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
formFile.CopyTo(stream);
|
||||
}
|
||||
|
||||
string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{filePath.Replace("\\", " /")}{fileName}";
|
||||
SysFile file = new()
|
||||
string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
|
||||
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, HttpContext.GetName())
|
||||
{
|
||||
AccessUrl = accessPath,
|
||||
Create_by = HttpContext.GetName(),
|
||||
FileExt = fileExt,
|
||||
FileName = fileName,
|
||||
FileSize = fileSize + "kb",
|
||||
StoreType = 1,
|
||||
FileUrl = finalFilePath,
|
||||
RealName = formFile.FileName,
|
||||
Create_time = DateTime.Now,
|
||||
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
|
||||
FileType = formFile.ContentType
|
||||
};
|
||||
long fileId = SysFileService.InsertFile(file);
|
||||
@ -183,17 +174,9 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
|
||||
});
|
||||
long id = SysFileService.InsertFile(new SysFile()
|
||||
long id = SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
|
||||
{
|
||||
AccessUrl = result.Item2,
|
||||
Create_by = HttpContext.GetName(),
|
||||
FileExt = fileExt,
|
||||
FileName = result.Item3,
|
||||
FileSize = fileSize + "kb",
|
||||
StoreType = 2,
|
||||
StorePath = fileDir,
|
||||
RealName = formFile.FileName,
|
||||
Create_time = DateTime.Now,
|
||||
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
||||
FileType = formFile.ContentType
|
||||
});
|
||||
return SUCCESS(new
|
||||
|
||||
@ -70,5 +70,18 @@ namespace ZR.Model.System
|
||||
/// 空值 : true
|
||||
/// </summary>
|
||||
public string AccessUrl { get; set; }
|
||||
|
||||
public SysFile() { }
|
||||
public SysFile(string realName, string fileName, string ext, string fileSize, string storePath, string accessUrl,string create_by)
|
||||
{
|
||||
StorePath = storePath;
|
||||
RealName = realName;
|
||||
FileName = fileName;
|
||||
FileExt = ext;
|
||||
FileSize = fileSize;
|
||||
AccessUrl = accessUrl;
|
||||
Create_by = create_by;
|
||||
Create_time = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -113,7 +113,12 @@
|
||||
<el-form-item label="文件id">{{formView.id}}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="12">
|
||||
<el-form-item label="扩展名">{{formView.fileExt}}</el-form-item>
|
||||
<el-form-item label="文件类型">{{formView.fileType}}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="12">
|
||||
<el-form-item label="扩展名">
|
||||
<el-tag>{{formView.fileExt}}</el-tag>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="12">
|
||||
<el-form-item label="源文件名">{{formView.realName}}</el-form-item>
|
||||
@ -130,12 +135,9 @@
|
||||
<el-col :lg="12">
|
||||
<el-form-item label="创建人">{{formView.create_by}}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="24">
|
||||
<el-form-item label="存储路径">{{formView.fileUrl}}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="24" v-if="['.png','.jpg', '.jpeg'].includes(formView.fileExt)">
|
||||
<el-form-item label="预览">
|
||||
<el-image :src="formView.accessUrl" fit="contain"></el-image>
|
||||
<el-image :src="formView.accessUrl" fit="contain" style="width:100px"></el-image>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="24">
|
||||
@ -146,6 +148,9 @@
|
||||
</el-button>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :lg="24">
|
||||
<el-form-item label="存储路径">{{formView.fileUrl}}</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</el-form>
|
||||
</el-dialog>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user