Example #1
0
 public async Task addFile(string filename, string type, string position)
 {
     _dbContext.Uploads.Add(new Uploads
     {
         FileName = filename,
         Type     = type,
         Position = position
     });
     await _dbContext.SaveChangesAsync();
 }
Example #2
0
        public async Task <bool> PostUploadsAsync(IFormFile content, string filePath)
        {
            bool IsSuccess = false;

            try
            {
                string fileuploadPath = Path.Combine(Directory.GetCurrentDirectory(), filePath);

                if (!Directory.Exists(fileuploadPath))
                {
                    Directory.CreateDirectory(filePath);
                }

                Upload upload = new Upload();
                upload.Guid = Guid.NewGuid().ToString();

                var fileName = content.FileName;
                upload.LocalFileName = GetFileName(fileName) + "_" + upload.Guid + "." + GetFileExtension(fileName);

                upload.FileName = fileName;

                var fullPath = Path.Combine(fileuploadPath, upload.LocalFileName);
                upload.FilePath = Path.Combine(filePath, upload.LocalFileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    content.CopyTo(stream);
                    using (BinaryReader br = new BinaryReader(stream))
                    {
                        upload.FileBinary = br.ReadBytes((Int32)stream.Length);
                    }
                }

                upload.FileSize = content.Length;

                upload.UploadDate = DateTime.Now;

                _context.Uploads.Add(upload);
                await _context.SaveChangesAsync();

                IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
            }

            return(IsSuccess);
        }