1 Star 0 Fork 651

码神/OpenAuth.Core

forked from 李玉宝/OpenAuth.Core
暂停
 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FileApp.cs 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
李玉宝 提交于 2019-03-21 09:29 +08:00 . show file info
using System;
using System.Collections.Generic;
using System.IO;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
{
/// <summary>
/// 文件
/// </summary>
public class FileApp : BaseApp<UploadFile>
{
private ILogger<FileApp> _logger;
private string _filePath;
private string _dbFilePath; //数据库中的文件路径
private string _dbThumbnail; //数据库中的缩略图路径
public FileApp( IOptions<AppSetting> setOptions, IUnitWork unitWork, IRepository<UploadFile> repository, ILogger<FileApp> logger)
:base(unitWork, repository)
{
_logger = logger;
_filePath = setOptions.Value.UploadPath;
if (string.IsNullOrEmpty(_filePath))
{
_filePath = AppContext.BaseDirectory;
}
}
public List<UploadFile> Add(IFormFileCollection files)
{
var result = new List<UploadFile>();
foreach (var file in files)
{
result.Add(Add(file));
}
return result;
}
public UploadFile Add(IFormFile file)
{
if (file != null)
{
_logger.LogInformation("收到新文件: " + file.FileName);
_logger.LogInformation("收到新文件: " + file.Length);
}
else
{
_logger.LogWarning("收到新文件为空");
}
if (file != null && file.Length > 0 && file.Length < 10485760)
{
using (var binaryReader = new BinaryReader(file.OpenReadStream()))
{
var fileName = Path.GetFileName(file.FileName);
var data = binaryReader.ReadBytes((int)file.Length);
UploadFile(fileName, data);
var filedb = new UploadFile
{
FilePath = _dbFilePath,
Thumbnail = _dbThumbnail,
FileName = fileName,
FileSize = file.Length,
FileType = Path.GetExtension(fileName),
Extension = Path.GetExtension(fileName)
};
Repository.Add(filedb);
return filedb;
}
}
else
{
throw new Exception("文件过大");
}
}
private void UploadFile(string fileName, byte[] fileBuffers)
{
string folder = DateTime.Now.ToString("yyyyMMdd");
//判断文件是否为空
if (string.IsNullOrEmpty(fileName))
{
throw new Exception("文件名不能为空");
}
//判断文件是否为空
if (fileBuffers.Length < 1)
{
throw new Exception("文件不能为空");
}
var uploadPath = _filePath + "/" + folder + "/";
_logger.LogInformation("文件写入:" + uploadPath);
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
var ext = Path.GetExtension(fileName).ToLower();
string newName = GenerateId.GenerateOrderNumber() + ext;
using (var fs = new FileStream(uploadPath + newName, FileMode.Create))
{
fs.Write(fileBuffers, 0, fileBuffers.Length);
fs.Close();
//生成缩略图
if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") || ext.Contains(".gif"))
{
string thumbnailName = GenerateId.GenerateOrderNumber() + ext;
ImgHelper.MakeThumbnail(uploadPath + newName, uploadPath + thumbnailName);
_dbThumbnail = folder + "/" + thumbnailName;
}
_dbFilePath = folder + "/" + newName;
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/dotcpp/OpenAuth.Core.git
git@gitee.com:dotcpp/OpenAuth.Core.git
dotcpp
OpenAuth.Core
OpenAuth.Core
master

搜索帮助