1

I currently have a razor page that has a Uploader component (via Syncfusion) that calls an API whenever a file is uploaded. The API is responsible for renaming that file to the same name of the file that the UI is using inside of the wwwroot/assets folder, and rewriting whatever image data that the original image contains. The controller is not hit when I run in IIS, so I run the project using the API project associated with the Web Assembly project. This works exactly as expected and in my Web Assembly project, the image is replaced as it should be. The issue is is that in the UI, the image stays the same, and I think it has to do with the project I am running it on, which is the API project. However, the API project's wwwroot folder is located inside of the bin/net 5.0/6.0 folders.

This method is what I use to overwrite the image in the Web Assembly wwwroot folder. But because I am running from the API project, its hard to tell where the png is coming from.

     [HttpPost("[action]")]
        public void SaveLogo([FromForm] IEnumerable<IFormFile> UploadFiles)
        {
            try
            {
                foreach (var file in UploadFiles)
                {


                        var filename = ContentDispositionHeaderValue
                            .Parse(file.ContentDisposition)
                            .FileName
                            .Trim('"');
                        filename = "KFNBOfficialLogo.png";
                        _hostingEnv.ContentRootPath = "C:\\Users\\user\\Desktop\\repos\\Manager\\Manager\\Manager\\wwwroot\\assets\\Logo.png";

                        filename = _hostingEnv.ContentRootPath;

                        using (FileStream fs = System.IO.File.Create(filename))
                        {
                            file.CopyTo(fs);
                            fs.Flush();
                        }

                }
            }
            catch (Exception e)
            {
                Response.Clear();
                Response.StatusCode = 204;
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
            }
        }

    }
  • 1
    You're doing it wrong. Those are called static files for a reason. They aren't supposed to change. If you have a dynamic file, you need to serve that file from a controller method. Static files are cached by the web browser and will not be replaced unless you hard-refresh the browser page. – John Lord Jun 20 '23 at 21:11
  • if you want to force cache refresh of specific file, you may append timestamp parameter to the path on UI. example: /logo.png?t=123456. When file is updated, UI should use /logo.png?t=7890123 – Yehor Androsov Jun 20 '23 at 21:19
  • @JohnLord, that makes sense. However, after the file changes, and upon exiting the program and browser, reloading everything will result in the same, unwanted, png being on the UI. Is this still a cache issue? – AgeOFUltron Jun 20 '23 at 21:47
  • in that case i would verify manually by loading the file yourself from the file system that it's actually being overwritten. It's quite possible you don't have write permission and it's failing. – John Lord Jun 21 '23 at 15:40

0 Answers0