public async Task UploadFiles(StreamStorageFile files, string userName) { // User Profile var profile = await _userManager.FindByNameAsync(userName); // Accumulate the form data key-value pairs in the request (formAccumulator). var trustedFileNameForDisplay = string.Empty; var untrustedFileNameForStorage = string.Empty; var trustedFilePathStorage = string.Empty; var trustedFileNameForFileStorage = string.Empty; // Test Files List <ClamUserPersonalCategoryItem> userFiles = new List <ClamUserPersonalCategoryItem>(); foreach (var file in files.File) { untrustedFileNameForStorage = file.FileName; // Don't trust the file name sent by the client. To display // the file name, HTML-encode the value. trustedFileNameForDisplay = WebUtility.HtmlEncode( file.FileName); if (!Directory.Exists(_targetFilePath)) { string path = String.Format("{0}", _targetFilePath); Directory.CreateDirectory(path); } // Bind form data to the model var keyPathFolder = FilePathUrlHelper.GenerateKeyPath(profile.Id); var generateKeyFolder = GenerateSecurity.Encode(profile.Id); trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}", _targetFolderPath, keyPathFolder, generateKeyFolder, Path.GetRandomFileName()); Directory.CreateDirectory(trustedFilePathStorage); using (var fileStream = new FileStream(Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage), FileMode.Create, FileAccess.Write)) { await file.CopyToAsync(fileStream); fileStream.Close(); } userFiles.Add(new ClamUserPersonalCategoryItem() { FileName = trustedFileNameForDisplay, ItemPath = Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage), Size = file.Length, UserId = profile.Id, DateCreated = DateTime.Now, LastModified = DateTime.Now }); } Task.WaitAll(_context.AddRangeAsync(userFiles)); Task.WaitAll(_context.SaveChangesAsync()); }
public async Task <StreamStorageFile> GetStorageHome(string userName) { var userProfile = await _userManager.FindByNameAsync(userName); // Load Model StreamStorageFile FileHome = new StreamStorageFile(); // Retrieve data to display long totalFileSizeCount = 0; List <AreaUserPersonalCategoryItems> displyFilesOfUser = new List <AreaUserPersonalCategoryItems>(); var userFiles = await _context.ClamUserPersonalCategoryItems.ToListAsync(); foreach (var file in userFiles) { if (file.UserId.Equals(userProfile.Id)) { displyFilesOfUser.Add(new AreaUserPersonalCategoryItems() { ItemId = file.ItemId, FileName = file.FileName, ItemPath = file.ItemPath, Size = file.Size, FileType = Path.GetExtension(file.FileName), DateCreated = file.DateCreated }); totalFileSizeCount += file.Size; } } FileHome.AreaUserPersonalCategoryItems = displyFilesOfUser; return(FileHome); }
public async Task <IActionResult> MultipleFiles(StreamStorageFile files) { // User Profile var name = User.Identity.Name; var profile = await _userManager.FindByNameAsync(name); // Accumulate the form data key-value pairs in the request (formAccumulator). var trustedFileNameForDisplay = string.Empty; var untrustedFileNameForStorage = string.Empty; var trustedFilePathStorage = string.Empty; var trustedFileNameForFileStorage = string.Empty; var streamedFileImageContent = new byte[0]; var streamedFilePhysicalContent = new byte[0]; // List Byte for file storage List <byte[]> filesByteStorage = new List <byte[]>(); List <string> filesNameStorage = new List <string>(); List <string> storedPaths = new List <string>(); List <string> storedPathDictionaryKeys = new List <string>(); var fileStoredData = new Dictionary <string, byte[]>(); // Test Files List <ClamUserPersonalCategoryItem> userFiles = new List <ClamUserPersonalCategoryItem>(); foreach (var file in files.File) { untrustedFileNameForStorage = file.FileName; // Don't trust the file name sent by the client. To display // the file name, HTML-encode the value. trustedFileNameForDisplay = WebUtility.HtmlEncode( file.FileName); if (!Directory.Exists(_targetFilePath)) { string path = String.Format("{0}", _targetFilePath); Directory.CreateDirectory(path); } // Bind form data to the model var keyPathFolder = FilePathUrlHelper.GenerateKeyPath(profile.Id); var generateKeyFolder = GenerateSecurity.Encode(profile.Id); trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}", _targetFolderPath, keyPathFolder, generateKeyFolder, Path.GetRandomFileName()); Directory.CreateDirectory(trustedFilePathStorage); using (var fileStream = new FileStream(Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage), FileMode.Create, FileAccess.Write)) { await file.CopyToAsync(fileStream); fileStream.Close(); } userFiles.Add(new ClamUserPersonalCategoryItem() { FileName = trustedFileNameForDisplay, ItemPath = Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage), Size = file.Length, UserId = profile.Id, DateCreated = DateTime.Now, LastModified = DateTime.Now }); } ToDatabase(userFiles); return(RedirectToAction(nameof(Index))); }