File Upload Control Session does not remove after remove file from list

<svg aria-hidden="true" class="svg-icon iconClock" width="18" height="18" viewbox="0 0 18 18"><path d="M9 17A8 8 0 119 1a8 8 0 010 16zm0-2A6 6 0 109 3a6 6 0 000 12zM8 5h1.01L9 9.36l3.22 2.1-.6.93L8 10V5z"></path></svg> <b>The bounty expires in 5 days</b>. Answers to this question are eligible for a +100 reputation bounty. Eng Soon Cheah is looking for a <b>canonical answer</b>: <blockquote> Need to include Source code and explanation. </blockquote> </aside>

I had frustrated for a few week for this issue,

How can I do session for this Multiple File Upload

if (Session["FileUpload1"] == null && FileUploadQ2.HasFile) { Session["FileUpload1"] = FileUploadQ2; foreach (HttpPostedFile file in FileUploadQ2.PostedFiles) { listofuploadedfiles.Text += String.Format("

<font color='black'>" + file.FileName + "</font><a class='close'><font color='red'>x</font><a>" + "

"); } } else if (Session["FileUpload1"] != null && (!FileUploadQ2.HasFile)) { FileUploadQ2 = (FileUpload)Session["FileUpload1"]; } else if(FileUploadQ2.HasFile) { Session["FileUpload1"] = FileUploadQ2; }


As @VDWWD pointed out, you should not store files in the Session object - store them on disk instead. Below you can find fast implementation of that feature.

<pre class="lang-cs prettyprint-override"> public interface IFileStorage { Task UploadFile(string fileName, Stream fileContent); void TryRemoveFile(string fileName, out bool fileRemoved); void GetFile(string fileName); } public class FileSystemStorage : IFileStorage { private readonly PhysicalFileProvider _fileProvider; private readonly ILogger<FileSystemStorage>_logger; public FileSystemStorage(IFileProvider fileProvider, ILogger<FileSystemStorage> logger) { _fileProvider = (PhysicalFileProvider)fileProvider; _logger = logger; } public void GetFile(string fileName) { throw new NotImplementedException(); } public void TryRemoveFile(string fileName, out bool fileRemoved) { try { RemoveFile(fileName); fileRemoved = true; } catch(Exception ex) { _logger.LogError($"Couldnt remove file {fileName}: {ex.ToString()}" ); fileRemoved = false; } } public async Task UploadFile(string fileName, Stream fileContent) { var filePath = Path.Combine(_fileProvider.Root, fileName); if (_fileProvider.GetFileInfo(filePath).Exists) throw new ArgumentException("Given file already exists!"); _logger.LogInformation($"Saving file to: {filePath}.."); using (Stream file = File.Create(filePath)) { await fileContent.CopyToAsync(file); file.Flush(); } _logger.LogInformation($"File: {filePath} saved successfully."); } }

In Startup.cs, ConfigureServices method add below lines to inject FileSystemStorage:

IFileProvider physicalProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "AppData")); services.AddSingleton<IFileProvider>(physicalProvider); services.AddTransient<IFileStorage, FileSystemStorage>();

Then in your controller constructor obtain the FileSystemStorage instance.



  • File Upload Control Session does not remove after remove file from list
  • Refreshing dags without web server restart apache airflow
  • JavaScript Regex Ignore Case
  • Access Cookies from Twig?
  • How do I fix 'Source not found' error when debugging in Eclipse, using Selenium WebDriver?
  • Each simulation of normal distribution is the same (C++)
  • Laravel “envoy run” command not working with ssh key
  • Android LinearLayout Line Breaks in XML
  • How can I find the difference between two times [duplicate]
  • On unchecking a checkbox trigger an event
  • How to debug iBeacons and Passbook
  • Jenkins sending notifications to the wrong commit id
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • Smarter Removing Unnecessary WhiteSpace CSV
  • Issues with converting data into a matrix after running lapply()
  • Showing image on a acro text field position
  • How to control xtics in gnuplot
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • Splitting ReportLab table across PDF page (side by side)?
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • When to use the tag in the head and body section of a html page? [duplicate]
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to turn off notice reporting in xampp?
  • How to change user identity when git pushing via ssh?
  • Send array to next viewcontroller iOs xcode [duplicate]
  • php “page caching” solution suggestions for CMS Applications
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • Grails - How to implement a foreign key relationship not using an id column?
  • Codeigniniter insert data through models and controller
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • How to use FirstOrDefault inside Include
  • Firebase: How to read from external DB?
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • convert json to excel in java
  • ReferenceError: TextEncoder is not defined