Advertisement
yambroskin

Untitled

Dec 24th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1.     private Attachment CreateBinaryFile(string fileName)
  2.         {
  3.             if (string.IsNullOrEmpty(fileName))
  4.                 return null;
  5.             if (!File.Exists(fileName))
  6.             {
  7.                 throw new FileNotFoundException("Файл не найден или нет доступа", fileName);
  8.             }
  9.             var mimeMappingService = Locator.GetServiceNotNull<IMimeMappingService>();
  10.            
  11.             var binaryFile = new BinaryFile {
  12.                 ContentType = mimeMappingService.GetTypeByExtension(Path.GetExtension(fileName)),
  13.                 Name = Path.GetFileName(fileName),
  14.                 ContentFilePath = fileName,
  15.                 CreateDate = DateTime.Now,
  16.             };
  17.             binaryFile.InitializeContentFilePath();
  18.             File.Copy(fileName, binaryFile.ContentFilePath);
  19.             var fileManager = Locator.GetServiceNotNull<IFileManager>();
  20.             fileManager.SaveFile(binaryFile);
  21.             return new Attachment() {
  22.                 File = binaryFile
  23.             };
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement