0

I'm working on a web app using Asp.Net MVC and C#. Users are allowed to upload files which are done via jQuery ajax. What I am trying to do is add each file name a user uploads to a List<string>. For that I have the following code:

public class FileUploadController : Controller
{
    private List<string> UploadedFileNames = new List<string>();

    public List<string> AddFileNameToList(string fileName)
    {
        UploadedFileNames.Add(fileName);

        return UploadedFileNames;
    }

    public JsonResult UploadFile(string fileName)
    {

        //save file to directory etc

        AddFileNameToList(fileName);

        return Json(new {});//relevant info for the user
    }
}

With the above code each time I upload a file and add the filename to UploadedFileNames the previous count is always 0 rather than the all including all the previous filenames.

Can someone tell me how to achieve this please.

Didix
  • 567
  • 1
  • 7
  • 26
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • 5
    The MVC Controller instance `FileUploadController` (and any other Controller instance) is very short-living. The instance will only be used once per request, the list cannot survive within it. You need to store the List somewhere like a _Session_. – thmshd Feb 09 '18 at 10:23
  • 1
    You are creating a new list everytime, save it on Session and check if list exists in session, use that list, otherwise create one. – João Silva Feb 09 '18 at 10:24
  • @thmshd would it be possible if you can provide an example please – Izzy Feb 09 '18 at 10:24
  • @Code if you are saving the files according to each user, make a service that retrieves the list whenever it is required. – inthevortex Feb 09 '18 at 10:28
  • 2
    @Code yes, but this is something you should easily find in the Internet. But [here are some basics](https://stackoverflow.com/a/14139064/265165) to start – thmshd Feb 09 '18 at 10:29
  • @thmshd Thanks, I will have a look at that. – Izzy Feb 09 '18 at 10:29

3 Answers3

1

A new Controller instance is created for each request. and as a result of this your list is a new instance for each request. you will need to persist this list in another location.

If you only need to information for the period of time the user is on the site you could use a session.

however as it file uploads I suspect you may want to store the information more long term so returning users can see what they uploaded. In this case persist the information to a database on each request.

Chao226
  • 332
  • 2
  • 10
0

You could add the List to the session with code similar to this:

if (Session["UploadedFileNames"] != null)
{
    Session["UploadedFileNames"] = new List<string>() { fileName };           
}
else
{
    var list = (Session["UploadedFileNames"] as List<string>);
    list.Add(fileName);
    Session["UploadedFileNames"] = list;
}
S.Dav
  • 2,436
  • 16
  • 22
-3

Problem is probably that FileuploadController class initializes each time user tries to upload file, so UploadedFilenames list will reset to empty list on each upload, Make UploadedFileNames field static to prevent that.

Dzliera
  • 646
  • 5
  • 15
  • Making it `static` opens up a whole load of other problems, not least of which the fact that it then becomes not thread safe – Liam Feb 09 '18 at 10:29
  • Please don't. We're talking about a Web Application, I doubt you want all users to manage the same list – thmshd Feb 09 '18 at 10:32