1

So I asked this question a while ago and it got immediately closed so I'll give more detail the issue seems to be specific to the dmg file type I've never encountered the problem till now.

I simply want to be able to click a button and then have the dmg downloaded without using much javascript or any.

However using my code the dmg file opens in the browser so you get a bunch of binary gibberish and the file doesn't download if you want to see what I mean head to https://lucas-testing.000webhostapp.com/release then click download for MacOS.

The dmg's file name changes as the version number is part of the name so I use php to get the file information and create a variable storing that website address for example the current link generated by my PHP would be:

var downloadLinkAlt = "https://lucas-testing.000webhostapp.com/release/The City Of Truro Mariners - Management Console-1.0.19.dmg"

My code to then download this link is:

<button onclick="location.href=downloadLinkAlt" id="DownloadByOSAlt" class="btn btn-sm btn-primary mt-3">Download for MacOS</button>

I know for a fact the above code works as it works perfectly with the exact same link but with an exe file extension instead.

Things I have tried:

The download attribute that can be used in HTML5 to force download things like PDF's:

<button onclick="location.href=downloadLinkAlt" id="DownloadByOSAlt" class="btn btn-sm btn-primary mt-3" download>Loading...</button>

Reference: https://www.w3schools.com/tags/att_a_download.asp

Putting the button in a form and using a get request with the link:

<form method="get" action=downloadLinkAlt>
   <button type="submit">Download!</button>
</form>

Reference: https://stackoverflow.com/a/11620761/12887221

Using an invisible iframe that's source is changed when the button is pressed:

document.getElementById('my_iframe').src = downloadLinkAlt;

<iframe id="my_iframe" style="display:none;"></iframe>

Reference: https://stackoverflow.com/a/22231021/12887221

Does anyone have any suggestions as to what to try next?

futurelucas4502
  • 310
  • 3
  • 10

1 Answers1

0

It may not be that your methods of performing the download are broken but rather that the webserver isn't serving up the right mime type for the dmg file. That the file is downloading and you're getting what looks like gibberish could be a sign of that. The browser doesn't know what to expect.

To see what mime type the server is sending open up developer tools in your browser, click on network and look at the file you're downloading in the list.

Just googling around seems to indicate that the right mime type for DMG files is application\octet-stream. If it doesn't say this type when you look at it in developer tools then this is likely your problem. If it does then its probably something else. Here's an article on mime types: https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Configuring_server_MIME_types. Scroll down to the bottom for some notes on configuring mime types different web servers (Apache, IIS, etc).

Also noticing that your URL contains spaces. Those should be percent encoded to be valid.

Yet another perhaps more direct way of doing the download would just be to use a hyperlink like:

<a href="URL_to_your_flle.dmg">Download</a>

Which you could style to look like a button.

  • Using the a tag styled as a button also doesn't work could you provide more information about how to set the mime type? – futurelucas4502 Apr 23 '20 at 07:44
  • Sure no problem. Made some edits. Setting the mime time is dependent on the web server you're using. What does the network tab of developer tools in your browser tell you about the mime type of the file? If it says application/octet-stream then your problem may be something else. If it says something other than that, then I'd investigate setting the mime type which is probably a different SO question for whatever webserver you have. – Steve Ellis Apr 23 '20 at 12:57
  • Oh and don't forget to remove those spaces from your URL. – Steve Ellis Apr 23 '20 at 13:37