0

As the title mentions, I'm working on a website which uses jQuery. Due to changing user requirements, we now need to let the user download files where before he could only view them. I have the location(url) of the file that needs to be downloaded. Unfortunately I haven't had any luck regarding this as Internet Explorer is being ... well ... Internet Explorer.

My example would be the download of a .xml file. Instead of showing the Save as dialog, it opens in Internet Explorer. For Word files on the other hand, the download is correct.

I have tried using a href with <a target="_blank" href=" + targetUrl + ">, which didn't help. Then I tried document.execCommand('SaveAs',true,fileUrl); which also didn't help.

When looking online, I could find no mention of any working solution which made me question whether or not this is even possible.

So my question would be whether or not you can force IE9 and 11 to show the Save As dialog regardless of user settings or not.

whodares
  • 676
  • 4
  • 11
  • 28
  • do you have access to the server-side? – Mr.7 Sep 15 '16 at 10:05
  • I don't have access to the server-side. This has to happen on the front-side. Server-side would make it easier :( – whodares Sep 15 '16 at 10:09
  • as you have xml url, why can't you read the content and write it in an xml file? – Mr.7 Sep 15 '16 at 10:12
  • Xml is only an example. I have several file types in this application, which are being determined somewhere else. I have no clue what I can expect coming out of it. I have also seen .doc, .xls, .xlsx, .jp(e)g, .... The list goes on on on. – whodares Sep 15 '16 at 10:14
  • May be you can use `downloadify` js library – Mr.7 Sep 15 '16 at 11:31
  • Thanks for the tip, but the client does not like the Flash-plugin. He wants it to use just html/css/javascript (and thus also jQuery). Talk about making it hard! – whodares Sep 15 '16 at 11:58
  • face it, IE 9 is old, stop supporting it like microsoft did and they will install a better browser. "The only way to truly force the web to embrace modern open standards is to invalidate old technology." – Endless Sep 15 '16 at 12:05
  • The only way you will be able to show a dialog on ie <= 9 and safari is by using content-disposition header. There is no client-side solution for those – Endless Sep 15 '16 at 12:06

2 Answers2

0

Add a id for element a.

var element = document.getElementById('a');
var oWin = window.open("yourlink", "_blank");
oWin.document.write('text');
oWin.document.close();
var success = oWin.document.execCommand('SaveAs', true, element.id)
  • Thanks, but it didn't help. It creates a text file with the word text in it. I only have the url of the file, not the actual file contents. – whodares Sep 15 '16 at 10:08
0

I am not sure that i understand right your question. So what about use this

<a href={file.downloadUrl} download={file.name}>{ file.name }</a> 
Ivan Mjartan
  • 1,125
  • 1
  • 12
  • 23
  • As mentioned in the question, I have used that and it didn't work. It gave me the situation as described with file types being opened directly in IE instead of giving the Save As Dialog. – whodares Sep 15 '16 at 10:12
  • I have not tested in IE but "download" attribute is important – Ivan Mjartan Sep 15 '16 at 10:20
  • IE doesn't seem to support the download attribute. I also have to get this working in IE9, so a full html5 solution is probably not doable either. – whodares Sep 15 '16 at 10:24
  • Do you have access to backend? If yes this may be could help Add headers to response http://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html – Ivan Mjartan Sep 15 '16 at 10:30
  • header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT'); header('Accept-Ranges: bytes'); // Allow support for download resume header('Content-Length: ' . filesize($path)); // File size header('Content-Type: application/pdf'); // Change the mime type if the file is not PDF header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog – Ivan Mjartan Sep 15 '16 at 10:31
  • Unfortunately I have no access to back-end. All I have to work this out is the front-end. – whodares Sep 15 '16 at 10:34
  • So i think that you have no chance to do it. I have tried it time ago. My be there is some workaround but i failed too – Ivan Mjartan Sep 15 '16 at 10:42