0

I have controller with 2 methods. First method should set variable and do something. Second method (called by ajax) should get value of this variable. So, I can't use TempData and Cache (because there are 2 different requests). How to solve it? Use Session? But session is disabled by default in asp.net mvc and I'm afraid to have unexpected behavior of the whole application if I enable it... Global variables are not approved too (because I need to get value of variable for concrete user)

EDIT: workflow is:

  1. client jquery script call Upload method controller and pass a huge file. This method is executing long time (i.e. 1 minute, no matter). This method generates the unique name of file and add record to DB (with this unique name)
  2. user can cancel uploading and click 'cancel'. It calls another server method (i.e. named Cancel), which should remove record from DB (added by Upload method) and remove uploaded file (or uploaded part of this file).

So, in Cancel method I need to know the unique name of file, which is being uploaded. I have to store this name in Upload method in any storage (i.e. Redis, DB etc) and remove from this storage after upload success (or in Cancel method, if Cancel was called)

1 Answer 1

2

Then I would suggest to keep the value in a cookie for that user if the data is not to sensitive.

If the data is sensitive you need to store if for example in the cache of the server. There you keep a record per user and then reuse that and discard it after you have done the processing. This works well when you have one instance of the website. When you don't you should look at for example a redis cache, sql server, azure table storage, ...

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

5 Comments

I can't use cookie in my case. First method can't return anything to client (really, it's uploading a huge file with cancel ability)
Can you explain the flow maybe in more detail?
I have added details in the main question
What you could do is add a token when you upload your file. this token you keep in your user session or cookie. When you want to cancel the upload you just send the token to the server and let the server handle it. there is a similar question here that could help you with some code stackoverflow.com/questions/7729806/…
I thought about it. But client side control (blueimp) is not so flexible and I understood, that method 'Upload' should remove file and record in DB, not method 'Cancel' (because we can't know a stage of uploading in 'Cancel' method. It can be before adding record and after uploading, or before these 2 actions...). So, it would be better to set flag to remove in 'Cancel' method, but in the end of 'Upload' method check it whether flag is set and remove or not...

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.