How to update an image in an Asp.Net website when the Browser Cache is turned on.
If I display a picture on a web page and change it with the upload, then the new picture is not displayed because the browser has cached the previous picture in the cache.
Can I update the browser cache?
Image output
<img src="@Html.Raw(ViewData["Profil_Image"] />
|
Solution:
In asp mvc core, the image address is extended with a query parameter and a current time
Example:
<img src="@Html.Raw(ViewData["Profil_Image"] + "?" + DateTime.Now.Ticks)" />
|
In the website
<img src="/User_Files/User_Images/400/User_Image_1.jpg?636582684020015936" > |
Asp View, Asp Razor MVC Core
@{ ViewData["Title"] = "📝 User Profil Image" ; ViewData.AddActivePage(ManageNavPages.UserImage); }
<!DOCTYPE html>
<html> <head> <meta name="viewport" content="width=device-width" /> <title>Upload_Image</title> </head> <body> <span style="margin-top:10px;"> Profil Image:</span> <br /> <form asp-controller="Manage" asp-action="User_Profil_Image_Upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_File" /> <button type="submit" style="">Upload</button> </form>
<br /> @if (ViewData["Profil_Image"] !="") { <img src="@Html.Raw(ViewData["Profil_Image"] + "?" + DateTime.Now.Ticks)" alt="Uploaded Image" /> <br /> }
@if (ViewData["Error"] != null) { <label>Error= @ViewData["Error"]</label> }
<br /> </body> </html>
|