优化文件上传
This commit is contained in:
parent
c23b69a883
commit
b943a516e1
@ -39,16 +39,6 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
OptionsSetting = options.Value;
|
OptionsSetting = options.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 心跳
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpGet]
|
|
||||||
public IActionResult Health()
|
|
||||||
{
|
|
||||||
return SUCCESS(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// hello
|
/// hello
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -106,39 +96,16 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
[HttpPost()]
|
[HttpPost()]
|
||||||
[Verify]
|
[Verify]
|
||||||
[ActionPermissionFilter(Permission = "common")]
|
[ActionPermissionFilter(Permission = "common")]
|
||||||
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
|
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
|
||||||
{
|
{
|
||||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);
|
|
||||||
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);
|
|
||||||
double fileSize = formFile.Length / 1024.0;
|
|
||||||
|
|
||||||
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
|
SysFile file = await SysFileService.SaveFileLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
||||||
{
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var stream = new FileStream(finalFilePath, FileMode.Create))
|
|
||||||
{
|
|
||||||
formFile.CopyTo(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
|
|
||||||
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, HttpContext.GetName())
|
|
||||||
{
|
|
||||||
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
|
|
||||||
FileType = formFile.ContentType
|
|
||||||
};
|
|
||||||
long fileId = SysFileService.InsertFile(file);
|
|
||||||
return SUCCESS(new
|
return SUCCESS(new
|
||||||
{
|
{
|
||||||
url = uploadType == 1 ? finalFilePath : accessPath,
|
url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
|
||||||
fileName,
|
fileName,
|
||||||
fileId = fileId.ToString()
|
fileId = file.Id.ToString()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +141,11 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
|
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
|
||||||
});
|
});
|
||||||
long id = SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
|
if (!result.Item1)
|
||||||
|
{
|
||||||
|
return ToResponse(ApiResult.Error("阿里云连接失败"));
|
||||||
|
}
|
||||||
|
long id = await SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
|
||||||
{
|
{
|
||||||
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
||||||
FileType = formFile.ContentType
|
FileType = formFile.ContentType
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using Infrastructure.Attribute;
|
using Infrastructure.Attribute;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using ZR.Model.Models;
|
using ZR.Model.Models;
|
||||||
using ZR.Model.System;
|
using ZR.Model.System;
|
||||||
|
|
||||||
@ -7,15 +8,18 @@ namespace ZR.Service.System.IService
|
|||||||
{
|
{
|
||||||
public interface ISysFileService : IBaseService<SysFile>
|
public interface ISysFileService : IBaseService<SysFile>
|
||||||
{
|
{
|
||||||
long InsertFile(SysFile file);
|
Task<long> InsertFile(SysFile file);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 上传文件
|
/// 上传文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="picdir"></param>
|
/// <param name="fileDir"></param>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
/// <param name="formFile"></param>
|
/// <param name="formFile"></param>
|
||||||
/// <returns>结果、地址、文件名</returns>
|
/// <param name="rootPath"></param>
|
||||||
(bool, string, string) SaveFile(string picdir, IFormFile formFile);
|
/// <param name="userName"></param>
|
||||||
|
/// <returns>文件对象</returns>
|
||||||
|
Task<SysFile> SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile);
|
||||||
(bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName);
|
(bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 按时间来创建文件夹
|
/// 按时间来创建文件夹
|
||||||
|
|||||||
@ -11,6 +11,8 @@ using System.Net;
|
|||||||
using ZR.Model.System;
|
using ZR.Model.System;
|
||||||
using ZR.Repository.System;
|
using ZR.Repository.System;
|
||||||
using Infrastructure.Extensions;
|
using Infrastructure.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace ZR.Service.System
|
namespace ZR.Service.System
|
||||||
{
|
{
|
||||||
@ -22,26 +24,50 @@ namespace ZR.Service.System
|
|||||||
{
|
{
|
||||||
private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
|
private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
|
||||||
private readonly SysFileRepository SysFileRepository;
|
private readonly SysFileRepository SysFileRepository;
|
||||||
|
private OptionsSetting OptionsSetting;
|
||||||
public SysFileService(SysFileRepository repository)
|
public SysFileService(SysFileRepository repository, IOptions<OptionsSetting> options)
|
||||||
{
|
{
|
||||||
SysFileRepository = repository;
|
SysFileRepository = repository;
|
||||||
|
OptionsSetting = options.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 存储本地
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<SysFile> SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
|
||||||
|
{
|
||||||
|
string fileExt = Path.GetExtension(formFile.FileName);
|
||||||
|
string hashFileName = FileUtil.HashFileName();
|
||||||
|
fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
|
||||||
|
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
|
||||||
|
string filePath = FileUtil.GetdirPath(fileDir);
|
||||||
|
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
|
||||||
|
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
|
||||||
|
|
||||||
|
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var stream = new FileStream(finalFilePath, FileMode.Create))
|
||||||
|
{
|
||||||
|
await formFile.CopyToAsync(stream);
|
||||||
|
}
|
||||||
|
string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
|
||||||
|
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, userName)
|
||||||
|
{
|
||||||
|
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
|
||||||
|
FileType = formFile.ContentType,
|
||||||
|
FileUrl = finalFilePath
|
||||||
|
};
|
||||||
|
file.Id = await InsertFile(file);
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 上传文件到阿里云
|
/// 上传文件到阿里云
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="picdir"></param>
|
|
||||||
/// <param name="formFile"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public (bool, string, string) SaveFile(string picdir, IFormFile formFile)
|
|
||||||
{
|
|
||||||
return SaveFile(picdir, formFile, "", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 存储文件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="picdir">文件夹</param>
|
/// <param name="picdir">文件夹</param>
|
||||||
/// <param name="formFile"></param>
|
/// <param name="formFile"></param>
|
||||||
/// <param name="customFileName">自定义文件名</param>
|
/// <param name="customFileName">自定义文件名</param>
|
||||||
@ -87,11 +113,11 @@ namespace ZR.Service.System
|
|||||||
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public long InsertFile(SysFile file)
|
public Task<long> InsertFile(SysFile file)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Insertable(file).ExecuteReturnSnowflakeId();//单条插入返回雪花ID;
|
return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -73,7 +73,7 @@
|
|||||||
<el-row>
|
<el-row>
|
||||||
<el-col :lg="24">
|
<el-col :lg="24">
|
||||||
<el-form-item label="" prop="storeType">
|
<el-form-item label="" prop="storeType">
|
||||||
<el-radio-group v-model="form.storeType" placeholder="请选择存储类型" @change="handleSelectStore">
|
<el-radio-group v-model="form.storeType" placeholder="请选择存储类型">
|
||||||
<el-radio v-for="item in storeTypeOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">
|
<el-radio v-for="item in storeTypeOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">
|
||||||
{{item.dictLabel}}
|
{{item.dictLabel}}
|
||||||
</el-radio>
|
</el-radio>
|
||||||
@ -113,16 +113,18 @@
|
|||||||
<el-form-item label="文件id">{{formView.id}}</el-form-item>
|
<el-form-item label="文件id">{{formView.id}}</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :lg="12">
|
<el-col :lg="12">
|
||||||
<el-form-item label="文件类型">{{formView.fileType}}</el-form-item>
|
<el-form-item label="源文件名">{{formView.realName}}</el-form-item>
|
||||||
|
</el-col>
|
||||||
|
<el-col :lg="12">
|
||||||
|
<el-form-item label="文件类型">
|
||||||
|
<el-tag>{{formView.fileType}}</el-tag>
|
||||||
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :lg="12">
|
<el-col :lg="12">
|
||||||
<el-form-item label="扩展名">
|
<el-form-item label="扩展名">
|
||||||
<el-tag>{{formView.fileExt}}</el-tag>
|
<el-tag>{{formView.fileExt}}</el-tag>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :lg="12">
|
|
||||||
<el-form-item label="源文件名">{{formView.realName}}</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
<el-col :lg="12">
|
<el-col :lg="12">
|
||||||
<el-form-item label="文件名">{{formView.fileName}}</el-form-item>
|
<el-form-item label="文件名">{{formView.fileName}}</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
@ -230,6 +232,17 @@ export default {
|
|||||||
// 列表数据查询
|
// 列表数据查询
|
||||||
this.getList()
|
this.getList()
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
'form.storeType': {
|
||||||
|
handler: function(val) {
|
||||||
|
if (val == 1) {
|
||||||
|
this.uploadUrl = '/common/uploadFile'
|
||||||
|
} else if (val == 2) {
|
||||||
|
this.uploadUrl = '/common/UploadFileAliyun'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
// 查询数据
|
// 查询数据
|
||||||
getList() {
|
getList() {
|
||||||
@ -326,14 +339,6 @@ export default {
|
|||||||
storeTypeFormat(row, column) {
|
storeTypeFormat(row, column) {
|
||||||
return this.selectDictLabel(this.storeTypeOptions, row.storeType)
|
return this.selectDictLabel(this.storeTypeOptions, row.storeType)
|
||||||
},
|
},
|
||||||
handleSelectStore(val) {
|
|
||||||
this.queryParams.storeType = val
|
|
||||||
if (val == 1) {
|
|
||||||
this.uploadUrl = '/common/uploadFile'
|
|
||||||
} else if (val == 2) {
|
|
||||||
this.uploadUrl = '/common/UploadFileAliyun'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/** 复制代码成功 */
|
/** 复制代码成功 */
|
||||||
clipboardSuccess() {
|
clipboardSuccess() {
|
||||||
this.msgSuccess('复制成功')
|
this.msgSuccess('复制成功')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user