2

I am setting the window.location to download a file ("/foo/bar"). Now, the download works in google chrome, but it emits this warning:

Resource interpreted as Document but transferred with MIME type application/csv:

These are the response headers that are set according to google chrome:

Content-Description:File Transfer  
Content-Disposition:attachment; filename="foo.csv"  
Content-Length:29  
Content-Type:application/csv
Date:Sun, 14 Dec 2014 20:53:33 GMT  
Server:http-kit  

Any ideas if I can set the accept header on js side anyhow to omit this warning?

trooper
  • 4,444
  • 5
  • 32
  • 32
sveri
  • 1,372
  • 1
  • 13
  • 28

1 Answers1

3

You can use an anchor tag

<a href="fileLink" download="filename">Download</a>

make sure for the download attribute you put the extension type of the file like .html, .css, .js whatever it is

Edwin Reynoso
  • 1,511
  • 9
  • 18
  • But I don't have a link at all. I am doing a ajax request and afterwards set window.location. – sveri Dec 15 '14 at 08:53
  • create one dynamically? using javascript **document.createElement('a')** – Edwin Reynoso Dec 15 '14 at 08:54
  • And then triggering a click on the link via js? I have never done that before, but it sounds like it might work. – sveri Dec 15 '14 at 09:13
  • 2
    **var downloadLink = document.createElement('a'); downloadLink.href = "fileLink"; downloadLink.download = "fileName";** I believe the link has to be on the document so: **document.body.appendChild(downloadLink); downloadLink.click();** – Edwin Reynoso Dec 15 '14 at 09:15
  • This works, at least there is no warning anymore and I can download the file. Thank you. – sveri Dec 15 '14 at 15:27
  • 1
    Still attempt to see how to do it with the url that way you don't have to mess with the dom. also look up on **window.open** to download files – Edwin Reynoso Dec 15 '14 at 15:29