SamuelDavis
SamuelDavis

Reputation: 3324

How to persist a file between controllers using C# MVC?

I asked a previous question here and one part of it seems to be a whole subject in itself.

I am attempting to preserve a files data across controller calls.

The scenario is: A user uploads a file containing entries to be submitted to a database. If the file has warnings and errors they'll be redirected to a validation page. If there are no warnings or errors their file will be directly processed.

If the user receives warnings but no errors (ie. a text field is too long and will be truncated) they can still import the file. As there are two separate screens between uploading the file and sending it to the database the file data will need to be preserved.

I have seen a dozen similar questions to this but none of them seem to have relate-able answers (so please don't mark this question as a duplicate if a similar question doesn't have an answer to this exact problem).

My first idea was to save the file using the uploaded file name + a guid and saving that file name to the session state, but the answers I got on my previous question were all pretty adamant that this was a terrible solution.

The solutions I have found to other similar questions:

  1. Reupload the file (a terrible solution)
  2. Using a viewbag to send the file name to the view, to be sent back to the second controller (doesn't sound right)
  3. Save the file location in the view model to be sent to the view, to be retrieved again by the second controller (again doesn't sound right)
  4. Save the file location in the session or in temp data (receive generally unfavorable remarks)

I have done my research and cannot find the best way to go about this. Can someone please help me? If you think another question answers this, can you please send it to me as an answer instead of reporting this as a duplicate as I have already done quite a bit of searching.

Thank you, Samuel Davis

EDIT: my users do have a session available. Another solution I could think of is appending a hardcoded string to the GUID contained within their session ID eg. "MyImporter-xxxx.xxxx.xxxx.xxxx.csv". This way "MyImporter" could be a private constant string, and my controller wouldn't have to persist any identifier. I am unsure of any downsides to this method, but as I am pretty fresh to MVC there should certainly be cases that I haven't thought of.

Upvotes: 0

Views: 807

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

The best would be to store the location of the file on your server. Session or database.

Flow:

  1. User uploads a file
  2. The server stores the file in a some location on the server and stores the location of this file in a Session variable or in the Database
  3. The user sends a second request
  4. The server retrieves the location from the Session or the Database.
  5. The server does something with the file

It's worth mentioning that if you decide to store the file location in a database, you should also store the currently authenticated user in the same table so that you could later retrieve the correct record. If you are not using authentication that might not be a good solution because you will not be able to retrieve the record. In this case you could use a hard to guess identifier in the database to store the file location under. Then this identifier will be passed to the view (under the form of a view model of course) and passed on subsequent request. It is important that this identifier is difficult to guess to avoid one user working with the files of another user.

Another possible solution might be to store the location of the file in an encrypted cookie which will be sent on subsequent request.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190976

Have you heard of TempData? Its like a self-destructing session value.

Upvotes: 0

Related Questions