| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using Attribute;
- using Common;
- using Infrastructure;
- namespace Services
- {
- /// <summary>
- /// 上传文件Service业务层处理
- /// </summary>
- [AppService(ServiceType = typeof(IUploadService), ServiceLifetime = LifeTime.Transient)]
- public class UploadService : IUploadService
- {
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="file">文件流</param>
- /// <returns>上传文件到本地</returns>
- 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;
- }
- }
- }
|