Reputation: 3324
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:
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
Reputation: 1039110
The best would be to store the location of the file on your server. Session or database.
Flow:
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
Reputation: 190976
Have you heard of TempData
? Its like a self-destructing session value.
Upvotes: 0