以下方法均是个人,仅供参考

 public interface IFileHelper
{
/// <summary>
/// 保存文件 (返回 Test.jpg) 出错就返回 error|错误信息
/// </summary>
string SaveFile(IFormFile file, FileCategory fc);
bool DeleteFile(string fileName, FileCategory fc); }
public class FileHelper: IFileHelper
{
private readonly IHostingEnvironment _hostingEnv;
private static readonly Random _rdm = new Random();
public FileHelper(IHostingEnvironment env)
{
_hostingEnv = env;
}
/// <summary>
/// 保存文件(返回新文件名)
/// </summary>
/// <param name="file"></param>
/// <param name="fc"></param>
/// <returns></returns>
public string SaveFile(IFormFile file, FileCategory fc)
{
var path = GetUploadPath(fc);
var targetDirectory = Path.Combine(_hostingEnv.WebRootPath, string.Format(path));
//这里进行随机文件名
var fileName = GetRandName(file);
var savePath = Path.Combine(targetDirectory, fileName);
try
{
file.CopyTo(new FileStream(savePath, FileMode.Create));
//return Upload/NewsPhoto/Test.jpg
//返回文件名
return fileName;
}
catch
{
return "false";
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public bool DeleteFile(string fileName, FileCategory fc)
{
var path = GetUploadPath(fc);
var targetDirectory = Path.Combine(_hostingEnv.WebRootPath, string.Format(path));
//物理完整路径
var physicalPath = Path.Combine(targetDirectory, fileName);
try
{
File.Delete(physicalPath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 相对路径 /Upload/NewsPhoto/Test.jpg
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetFullPath(FileCategory fc,string fileName)
{
var path = GetUploadPath(fc);
return Path.Combine(string.Format(path), fileName);
}
/// <summary>
/// 获取到上传路径(Upload//CasePhoto//)
/// </summary>
/// <param name="fc"></param>
/// <returns></returns>
public static string GetUploadPath(FileCategory fc)
{
switch (fc)
{
case FileCategory.CasePhoto:return "Upload//CasePhoto//";
case FileCategory.NewsPhoto:return "Upload//NewsPhoto//";
case FileCategory.CompanyPhoto: return "Upload//CompanyPhoto//";
case FileCategory.PositionPhoto: return "Upload//PositionPhoto//";
case FileCategory.Partner: return "Upload//Partner//";
default:return "";
}
} public static string GetRandName(string oldFileName)
{
//获取后缀
var extension= GetExtensionWithDot(oldFileName);
//产生新的文件名
var newFileName = DateTime.Now.ToFileTime().ToString() + _rdm.Next();
//组合
return newFileName + extension;
} public static string GetRandName(IFormFile file)
{
var fileName = file.FileName;
var randName = GetRandName(fileName);
return randName;
} public enum FileCategory
{
/// <summary>
/// 案例文章插图
/// </summary>
CasePhoto,
/// <summary>
/// 新闻文章插图
/// </summary>
NewsPhoto,
/// <summary>
/// 公司介绍插图
/// </summary>
CompanyPhoto,
/// <summary>
/// 职位描述插图
/// </summary>
PositionPhoto,
/// <summary>
/// 合作伙伴
/// </summary>
Partner, }
/// <summary>
/// 获取到后缀名的方法
/// </summary>
public static string GetExtensionWithDot(string fileName)
{
var dotIndex = fileName.LastIndexOf('.');
if (dotIndex < || dotIndex >= fileName.Length) return string.Empty;
return fileName.Substring(dotIndex);
}
}

//添加一个FileHelper的依赖注入(依赖注入的实现类一定要写构造方法)
//该方法是单例模式

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IFileHelper, FileHelper>();
}

在startup中加入

新建一个UploadController 专门处理上传

public class UploadController : Controller
{
private readonly IFileHelper _fileHelper;
public UploadController(IFileHelper fileHelper)
{
_fileHelper = fileHelper;
}
/// <summary>
/// 专门为Case的文件上传 有可能有两个name 不支持H5 wangEditorFormFile 支持h5 wangEditorH5File
/// </summary>
/// <returns></returns>
[HttpPost]
public string UploadCompanyPhoto(IFormFile wangEditorFormFile = null, IFormFile wangEditorH5File = null)
{
//全部用/,windows支持/
return Upload(FileCategory.CompanyPhoto, wangEditorFormFile, wangEditorH5File);
} private string Upload(FileCategory fc ,IFormFile wangEditorFormFile = null, IFormFile wangEditorH5File = null)
{
if (wangEditorFormFile == null && wangEditorH5File == null)
{
return "error|无文件上传";
}
var file = wangEditorFormFile == null ? wangEditorH5File : wangEditorFormFile;
var result = _fileHelper.SaveFile(file, fc);
//判断是否错误
if (result == "false")
{
return "error|上传失败";
}
else
{
return "http://" + Request.Host.ToString() + "/" + FileHelper.GetFullPath(fc, result); ;
}
}
}

以上方法仅供参考

这是我新写的基于DotnetCore 1.1 写的一个简单的Demo : http://git.oschina.net/quan01994/UploadDemo

最新文章

  1. C语言计算2个数的最小公倍数
  2. Selenium IDE和Selenium RC的安装
  3. java 多线程(wait/notify/notifyall)
  4. three.js右手坐标系, 显示和线条
  5. HDOJ 1196 Lowest Bit(二进制相关的简单题)
  6. BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )
  7. Maven 构建浏览器解析userAgent类
  8. 突破防盗链Referrer
  9. Linux批量修改(删除)文件名某些字符(rename命令)
  10. Java框架spring Boot学习笔记(一):开始第一个项目
  11. java 项目的路径详情
  12. es6基本语法
  13. C++构造函数和析构函数,以及构造函数特殊成员变量和函数的初始化
  14. sas 经验小结(1)
  15. 配置Jsp错误页面
  16. Java数字签名算法--RSA
  17. 解决ImmediateDeprecationError 用Python获取Yahoo数据
  18. 每日英语:Yahoo&#39;s Rally: Made in China
  19. python---自动群发邮件
  20. linux下项目开发加载动态库:ldconfig与 /etc/ld.so.conf

热门文章

  1. 数据库触发器inserted和deleted详解
  2. poj 3262 Protecting the Flowers
  3. JSONObject put,accumulate,element的区别
  4. splitFile2SmallFile
  5. Html5shiv
  6. Java表单中关于JavaScript的非空必输验证,你的程序和大牛有多少差距╮( ̄▽ ̄&quot;)╭
  7. java中使用jxl导出Excel表格详细通用步骤
  8. IT girl
  9. 归并排序算法 java 实现
  10. ES6里箭头函数的陷阱