1

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("<p><font color='black'>" + file.FileName + "</font><a class='close'><font color='red'>x</font><a>" + "</p>");

            }

        }

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

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

6
  • What is it that you are trying to do exactly? A Control in a Session? Why? Commented May 14, 2020 at 12:38
  • @VDWWD a session. I want do like this codeproject.com/Tips/101834/… for multiple file Commented May 14, 2020 at 12:41
  • Just write the files to a temp folder. Session is not meant to store files! Commented May 14, 2020 at 13:32
  • @VDWWD if want to delete selected file? Commented May 14, 2020 at 13:54
  • Then delete it from the disk. Commented May 14, 2020 at 14:10

1 Answer 1

3
+100

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.

    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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have updated the answer so it uses the PhysicalFileProvider. Does it solves your issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.