0

I have a scenario where the user passes a fileName to download. We don't download the file on the server and stream back to the user because of bandwidth restrictions We get the file path to download, and redirect to the location where the json file would be hosted

        [Route("[controller]/DownloadJsonFile")]
        public async Task DownloadJsonFile(string fileName)
        {
            //Get the file name
            string fileToDownload = "https://hostedfilelocation/....test.json"
            Response.Redirect(fileToDownload);
        }

Currently, this method ends up rendering the Json content on the browser. Is there a way so that the browser can start automatically downloading the file? That way it wouldn't take super long to render the file on the browser.

P.S. If the file is of type zip or gzip, it is not rendered on the browser but rather is automatically downloaded. The application is a .Net 6 Asp.Net MVC application

I have tried the below code but the behavior is the same but it renders json on the browser instead of downloading it.

            string fileToDownload = "https://hostedfilelocation/....test.json"
            HttpResponse response = HttpContext.Response;
            response.Clear();       
            response.ContentType = "application/octet-stream";
            response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);                        
            Response.Redirect(fileToDownload);

The approaches mentioned in this blog post are all mentioning rendering the file in an iframe but I want the download happen on the client side.

Download File via browser redirect

  • Post your client code as well please, is the file server from you server or not? Redirect ignores the headers that you set, if you are not hosting the file and therefore not _serving_ the file then you can't change the headers on the file stream. Where are the files hosted? – Chris Schaller Dec 16 '22 at 03:06
  • Hi @ChrisSchaller The file is hosted on a Google Storage Bucket The client side code use jQuery Datatable where table row is bound this way ``` – the magician Dec 16 '22 at 03:19
  • The headers you have added there won't have an effect as you're essentially redirecting the user to another server. The issue here is more that you want the browser to download the file instead of rendering, so doesn't matter if it's a json file or a picture. Might require some javascript, try some of the suggestions here https://stackoverflow.com/questions/17527713/force-browser-to-download-image-files-on-click – J. Minjire Dec 20 '22 at 11:28
  • Yes. I tried to download it through JavaScript only to be denied by CORS policy violation. I am fairly confident if that could be bypassed, then everything would have worked. – the magician Dec 21 '22 at 21:56

1 Answers1

0

If you want to download it directly, add the download attribute:

<a class='download-file-link' target='_blank' href='DownloadJsonFile'  download="somefilename">

enter image description here

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11
  • That unfortunately did not work for me as expected. In my case, the file url would point to a google storage location which would return json. In your example, where is your appSettings.json file served from. What does your controller return? – the magician Dec 16 '22 at 14:06