Example #1
0
        /// <summary>
        ///   Save file Async and return its relative path
        /// </summary>
        /// <param name="fileDto"></param>
        /// <returns> relative path of file </returns>
        public async Task <string> SaveAsync(SavedFileDto fileDto)
        {
            try
            {
                var rootPath = _hostingEnvironment.WebRootPath;

                var fileName = fileDto.CanChangeName ? $"{Guid.NewGuid()}.{Path.GetExtension(fileDto.File.Name)}" : fileDto.File.Name;

                var attachmentPath   = Path.Combine(GetAttachmentTypePath(fileDto.attachmentType), fileDto.SubFolderName ?? "");
                var relativeFilePath = Path.Combine(attachmentPath, fileName);

                // Directory.CreateDirectory(Path.Combine(rootPath, attachmentPath));
                var fileDestinationPath = Path.Combine(rootPath, relativeFilePath);

                using (var fileStream = new FileStream(fileDestinationPath, FileMode.Create))
                {
                    await fileDto.File.CopyToAsync(fileStream);
                }

                return(getRelativeURL(relativeFilePath));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, string.Empty);
                return(string.Empty);
            }
        }
Example #2
0
        /// <summary>
        ///   Save file and return its relative path
        /// </summary>
        /// <param name="fileDto"></param>
        /// <returns> relative path of file </returns>
        public string Save(SavedFileDto fileDto)
        {
            try
            {
                var rootPath      = _hostingEnvironment.WebRootPath;
                var fileExtension = Path.GetExtension(fileDto.File.ContentType);
                fileExtension = string.IsNullOrEmpty(fileExtension) ? ".png" : fileExtension;
                var fileName = fileDto.CanChangeName ? $"{Guid.NewGuid()}{fileExtension}" : fileDto.File.Name;

                var attachmentPath   = Path.Combine(GetAttachmentTypePath(fileDto.attachmentType), fileDto.SubFolderName ?? "");
                var relativeFilePath = Path.Combine(attachmentPath, fileName);
                Directory.CreateDirectory(Path.Combine(rootPath, attachmentPath));
                var fileDestinationPath = Path.Combine(rootPath, relativeFilePath);

                using (var fileStream = new FileStream(fileDestinationPath, FileMode.Create))
                {
                    fileDto.File.CopyTo(fileStream);
                }

                return(getRelativeURL(relativeFilePath));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, string.Empty);
                return(string.Empty);
            }
        }
Example #3
0
 public static bool SaveTempInDb(TmpFileData req, out SavedFileDto dto)
 {
     using (var sc = Shell.GetScope())
     {
         var _uploadHandler = sc.ServiceProvider.GetRequiredService <IUploadedFilesHandler>();
         return(_uploadHandler.SaveTemp(req, out dto, null, null, true));
     }
 }
Example #4
0
 public static bool SaveTemp(TmpFileData req, long type, out SavedFileDto dto, bool db = false)
 {
     using (var sc = Shell.GetScope())
     {
         var _uploadHandler = sc.ServiceProvider.GetRequiredService <IUploadedFilesHandler>();
         return(_uploadHandler.SaveTemp(req, out dto, type, null, db));
     }
 }
 public override bool SaveTemp(TmpFileData req, out SavedFileDto dto, long?type = null, string folder = null, bool db = false)
 {
     dto = null;
     if (req?.TmpPath != null)
     {
         var save = req.MapTo <SaveAttachmentRequest>(false);
         save.AttachmentTypeId = type ?? 0;
         save.SaveInDb         = db;
         var att = SaveAttachment(save);
         if (att.IsSuccess && att.Data.TryGetValue("FileData", out object sv))
         {
             dto = (SavedFileDto)sv;
             return(true);
         }
     }
     return(false);
 }