0

I have a ASP.NET MVC application where I am showing some files in a html table, in which the user can download the file by clicking on it.

There is a requirement, once the user downloads the file, the file record in the table should not be shown anymore. When the user downloads the file, I mark it as downloaded by the user in the backend table, and if I refresh the page then the record wont be shown on the grid since it is not coming from the backend anymore.

The problem that I am facing is, once I return the File from the controller I cannot figure out a way to run the refresh JavaScript Method. The download link in the html table is a anchor tag.

Can anyone please point me to a solution.

Thanks

2
  • Couldnt you just refresh the whole page from the controller action, or have the table as a Partial View and just refresh the partial view with Javascript? Commented Aug 3, 2021 at 9:45
  • the table is in a partial view. The controller return a FileStreamResult. I need to call the JavaScript function which refresh this partial view once the file is downloaded. Commented Aug 3, 2021 at 12:40

1 Answer 1

1

I found a workaround solution for my problem. Below is the solution that I came up with after doing some searching.

  1. Add a cookie to the response before the file is returned. enter image description here

  2. When the file download starts, this cookie will be written to the browser. The file download is triggered when the user clicks on a anchor link on the page. So I then added a onclick event to the anchor tag and registered a JavaScript timer to run every 1 second using setInterval() method.

  3. When the timer ticks, it will check for the above cookie in the browser. I used the library https://github.com/js-cookie/js-cookie to check and get the cookie.

  4. If the cookie is present then I called my JavaScript function to refresh the html table and removed the cookie from the browser and then remove the JavaScript timer using clearInterval() method. enter image description here

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

Comments

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.