2

I have implemented a downloader servlet using java as stated:

Implementing a simple file download servlet

The requirement is, on the browser side, the file should be automatically downloaded, not triggered by any anchor click. On the ajax response, I call

document.location = "downloadServlet?fileId=foo"

The problem is, it works fine for all desktop browsers, android mobile devices. But when the device is a iPad tablet, both chrome and safari do not download the file, for example if the file is a text file, the content is dumped to the page and my working page is lost. How can I force that the file is downloaded and not the content is shown? Even if trying a zip file for example, then the browser of tablet shows a different page and corrupts the working page. What I want is the browser downloads it as if performed SaveAs for all types of files.

Because the document.location changes the whole page on the iPad, I changed it to an anchor, but because it should be triggered automatically, I trigger the click event by

<a href="downloadServlet?.." target="_blank" rel="noopener" id="downloadAnchor" />

document.getElementById("downloadAnchor").click()

But then the popup blockers prevent the click event.

benchpresser
  • 2,171
  • 2
  • 23
  • 41
  • 1
    In your servlet, try setting the response header Content-Disposition: attachment; filename=myfilename.myext – Palamino Mar 27 '19 at 17:16
  • Hi, I already added that but not working for iPad even if setting the disposition to attachment, when you download a text file its content is directly put to the initial page – benchpresser Mar 28 '19 at 09:11
  • I tried all options https://stackoverflow.com/questions/6520231/how-to-force-browser-to-download-file but ipad+safari always opens the content on the current tab. Opening a new tab/window is not possible due popup blocking mechanism of safari. https://stackoverflow.com/questions/44180931/javascript-window-open-not-working-in-safari-and-chrome does not help because the file is generated dynamically and we are not sure that we will download a file when clicking the link (business rule) – benchpresser Mar 28 '19 at 15:37
  • 1
    Try setting the content-type response header as application/octet-stream mime type – Palamino Mar 28 '19 at 15:47
  • ı tried application/octet-stream, force-download, x-download but ipad always shows in the same page without saveas. – benchpresser Mar 29 '19 at 06:24

0 Answers0