文件上传存储到阿里云新增自定义文件名

This commit is contained in:
不做码农
2022-01-14 20:27:23 +08:00
parent b8e4cd2421
commit 4fefb4282d
7 changed files with 26 additions and 17 deletions

View File

@@ -16,7 +16,7 @@ namespace ZR.Service.System.IService
/// <param name="formFile"></param>
/// <returns>结果、地址、文件名</returns>
(bool, string, string) SaveFile(string picdir, IFormFile formFile);
(bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName);
/// <summary>
/// 按时间来创建文件夹
/// </summary>

View File

@@ -10,6 +10,7 @@ using System.Security.Cryptography;
using System.Net;
using ZR.Model.System;
using ZR.Repository.System;
using Infrastructure.Extensions;
namespace ZR.Service.System
{
@@ -35,9 +36,13 @@ namespace ZR.Service.System
/// <returns></returns>
public (bool, string, string) SaveFile(string picdir, IFormFile formFile)
{
// eg: idcard/2020/08/18
return SaveFile(picdir, formFile, "");
}
public (bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName)
{
// eg: uploads/2020/08/18
string dir = GetdirPath(picdir.ToString());
string tempName = HashFileName();
string tempName = customFileName.IsEmpty() ? HashFileName() : customFileName;
string fileExt = Path.GetExtension(formFile.FileName);
string fileName = $"{tempName}{fileExt}";
string webUrl = $"{domainUrl}/{dir}/{fileName}";
@@ -46,7 +51,6 @@ namespace ZR.Service.System
return (statusCode == HttpStatusCode.OK, webUrl, fileName);
}
public string GetdirPath(string path = "")
{
DateTime date = DateTime.Now;