using Attribute;
using Common;
using Infrastructure;
namespace Services
{
///
/// 上传文件Service业务层处理
///
[AppService(ServiceType = typeof(IUploadService), ServiceLifetime = LifeTime.Transient)]
public class UploadService : IUploadService
{
///
/// 上传文件
///
/// 文件流
/// 上传文件到本地
public string getPicPath(IFormFile file)
{
var option = App.OptionsSetting;
string dir = option.Upload.LocalSavePath + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
string result = "";
try
{
string dir_c = Function.getPath("/" + dir);
if (!System.IO.Directory.Exists(dir_c))
{
System.IO.Directory.CreateDirectory(dir_c);
}
if (file.Length > 0)
{
var fileName = "MT" + Function.MD5_16(Guid.NewGuid().ToString()) + Path.GetExtension(file.FileName);
// if (!string.IsNullOrEmpty(fname))
// {
// fileName = fname + Path.GetExtension(file.FileName);
// }
var filePath = Path.Combine(dir_c, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
result = "/" + dir + fileName;
stream.Dispose();
stream.Close();
}
}
}
catch (Exception ex)
{
Utils.WriteLog(DateTime.Now.ToString() + ex.ToString(), "上传文件异常");
result = "";
}
return result;
}
}
}