新增加上传文件到阿里云
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
using Infrastructure.Attribute;
|
||||
using ZR.Model.System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace ZR.Service.System.IService
|
||||
{
|
||||
public interface ISysFileService
|
||||
{
|
||||
(bool, string) SaveFile(string picdir, IFormFile formFile);
|
||||
|
||||
/// <summary>
|
||||
/// 按时间来创建文件夹
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns>eg: 2020/11/3</returns>
|
||||
string GetdirPath(string path = "");
|
||||
|
||||
/// <summary>
|
||||
/// 取文件名的MD5值(16位)
|
||||
/// </summary>
|
||||
/// <param name="name">文件名,不包括扩展名</param>
|
||||
/// <returns></returns>
|
||||
string HashFileName(string str = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
using Infrastructure.Attribute;
|
||||
using ZR.Model.System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.IO;
|
||||
using ZR.Service.System.IService;
|
||||
using ZR.Common;
|
||||
using Infrastructure;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Net;
|
||||
|
||||
namespace ZR.Service.System
|
||||
{
|
||||
@@ -10,6 +17,57 @@ namespace ZR.Service.System
|
||||
[AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)]
|
||||
public class SysFileService : ISysFileService
|
||||
{
|
||||
private string domainUrl = ConfigUtils.Instance.GetConfig("ALIYUN_OSS:domainUrl");
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件到阿里云
|
||||
/// </summary>
|
||||
/// <param name="picdir"></param>
|
||||
/// <param name="formFile"></param>
|
||||
/// <returns></returns>
|
||||
public (bool, string) SaveFile(string picdir, IFormFile formFile)
|
||||
{
|
||||
// eg: idcard/2020/08/18
|
||||
string dir = GetdirPath(picdir.ToString());
|
||||
string tempName = HashFileName();
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
string fileName = $"{tempName}{fileExt}";
|
||||
string webUrl = $"{domainUrl}/{dir}/{fileName}";
|
||||
|
||||
HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), Path.Combine(dir, fileName));
|
||||
|
||||
if (statusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return (true, webUrl);
|
||||
}
|
||||
return (false, "");
|
||||
}
|
||||
|
||||
public string GetdirPath(string path = "")
|
||||
{
|
||||
DateTime date = DateTime.Now;
|
||||
int year = date.Year;
|
||||
int month = date.Month;
|
||||
int day = date.Day;
|
||||
//int hour = date.Hour;
|
||||
|
||||
string timeDir = $"{year}/{month}/{day}";// date.ToString("yyyyMM/dd/HH/");
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
timeDir = path + "/" + timeDir;
|
||||
}
|
||||
return timeDir;
|
||||
}
|
||||
|
||||
public string HashFileName(string str = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str))
|
||||
{
|
||||
str = Guid.NewGuid().ToString();
|
||||
}
|
||||
MD5CryptoServiceProvider md5 = new();
|
||||
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user