小型文件系统,可上传,可浏览;
支持图片、视频、音频
[HttpGet]
[Route("CheckService")]
public string CheckService()
{
return "文件服务正常运行!";
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileinfo">文件信息</param>
/// <returns></returns>
[HttpPost]
[Route("UploadFile")]
public JsonResult UploadFile([FromForm] UploadFileInfo fileinfo)
{
UploadResult rt = new UploadResult();
try
{
//文件保存目录路径
string saveTempPath = "web\\" + fileinfo.AppCode + "\\" + fileinfo.FileType + "\\" + fileinfo.CreateDate+"\\";
//判断目录是否存在
string dirTempPath = this._evn.ContentRootPath + saveTempPath;
//文件保存路径
string filepath = dirTempPath + fileinfo.ID + fileinfo.Ext;
if (!Directory.Exists(dirTempPath))
{
Directory.CreateDirectory(dirTempPath);
}
var files = Request.Form.Files;
if(files.Count==0)
{
rt.Sucess = false;
rt.ErroInfo = "未上传文件!";
return Json(rt);
}
//已存在,删除原文件
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
}
using (var stream = new FileStream(filepath, FileMode.Create))
{
files[0].CopyTo(stream);
stream.Close();
}
rt.Sucess = true;
rt.SavePath = filepath;
}
catch (Exception ex)
{
rt.Sucess = false;
rt.ErroInfo = "UploadFile Exception:" + ex.Message;
this._logger.LogError(ex, "文件上传异常:", ex.Message);
}
rt.End();
return Json(rt);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
[HttpPost]
[Route("DeleteFile")]
public JsonResult DeleteFile([FromBody] UploadFileInfo fileInfo)
{
UploadResult rt = new UploadResult();
//文件保存目录路径
string saveTempPath = "web\\" + fileInfo.AppCode + "\\" + fileInfo.FileType + "\\" + fileInfo.CreateDate + "\\";
//判断目录是否存在
string dirTempPath = this._evn.WebRootPath + saveTempPath;
//文件保存路径
string filepath = dirTempPath + fileInfo.ID + fileInfo.Ext;
//已存在,删除原文件
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
rt.Sucess = true;
}
else
{
rt.Sucess = false;
rt.ErroInfo = "文件不存在!"+ filepath;
}
rt.End();
return Json(rt);
}
public class UploadFileInfo
{
/// <summary>
/// 文件ID
/// </summary>
public string ID { get; set; }
/// <summary>
/// 扩展名 .jpg/.png .mp4/.avi .mp3 .pdf
/// </summary>
public string Ext { get; set; }
/// <summary>
/// 显示名称
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// 应用编码 JHNIS
/// </summary>
public string AppCode { get; set; }
/// <summary>
/// 文件类型 图片 Image 文件 PDF 视频 Video 声音Sound
/// </summary>
public string FileType { get; set; }
/// <summary>
/// 创建日期 yyMMdd
/// </summary>
public string CreateDate { get; set; }
}
using JHNIS.CloudStore.WebApi.Controllers;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//添加HttpContext
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
//启用静态文件
app.UseStaticFiles();
//发布时服务器注册静态资源
string fileUpload = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "web");
if (!Directory.Exists(fileUpload))
{
Directory.CreateDirectory(fileUpload);
}
//pdf.js 读取properties 404的问题
var providerContent = new FileExtensionContentTypeProvider();
providerContent.Mappings[".properties"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(fileUpload),
RequestPath = "/web",
ContentTypeProvider= providerContent
});
app.Run();