Home > Software design >  How to download a file through HTML?
How to download a file through HTML?

Time:10-05

Simply put, I want to make an icon button with text that downloads a file when the user clicks it, and my html code isn't doing that. The twist is, I have an icon button elsewhere on my page to do that exact same thing, and that one works.

The reason I'm including this ability twice in my page is because I want the user to be able to download this file no matter where they are in the page. The icon-button-with-text is the expected go-to place to get the file because it has an icon and text explaining what the button does. Here's its example code:

button {
  cursor: pointer;
  height: 56px;
  width: 214px;
  margin-bottom: 5%;
  border-radius: 4px;
  font-family: sans-serif;
  font-size: 16px;
  line-height: 56px;
  filter: drop-shadow(2px 2px 4px rgb(0 0 0/0.75));
}

.button1 {
  background: none;
  border: none;
  outline: 2px black solid;
  padding-left: 8.2%;
}

.button1 a {
  color: black;
}

.button1 a:hover {
  cursor: pointer;
}

.button1 span {
  position: absolute;
  top: 0px;
  left: 42px;
}

.activeState {
  display: none;
}

.inactiveState {
  position: absolute;
  height: 18px;
  width: 18px;
  top: 8px;
  left: 16px;
}

.button1:active .activeState {
  display: inline-block;
  position: absolute;
  height: 18px;
  width: 18px;
  top: 8px;
  left: 16px;
}

.button1:active .inactiveState {
  display: none;
}
<button >
  <a href="files\downloadableFile.pdf" download>
    <img  src="graphics\downloadFile_inactive.svg">
    <img  src="graphics\downloadFile_active.svg">
    <span>    
      Download File
    </span>
  </a>
</button>

However, the icon-button-with-text is part of the body content, and so will scroll up and out of sight as the user goes through the page. So that the user can download the file no matter where they are in the page, I made an icon-button in my fixed top app bar. Here's its example code:

li {
  list-style-type: none;
}

.icon {
  width: 24px;
  height: 24px;
  padding-top: 8px;
  text-align: center;
}

.inactiveState {
  height: 24px;
  width: 24px;
  top: 16px;
  filter: drop-shadow(2px 2px 4px rgb(0 0 0/0.75));
}

.activeState {
  display: none;
  height: 24px;
  width: 24px;
  top: 16px;
  margin-left: 12px;
  filter: drop-shadow(2px 2px 4px rgb(0 0 0/0.75));
}

li:active .inactiveState {
  display: none;
}

li:active .activeState {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

li:hover {
  cursor: pointer;
  background: none;
  outline: 2px black solid;
  border-radius: 4px;
}
<li >
  <a href="files\downloadableFile.pdf" download>
    <img  src="icons\downloadFile_inactive.svg">
    <img  src="icons\downloadFile_active.svg">
  </a>
</li>

The icon-button was part of a menu of other links, so I made it a list item instead of an actual button.

Both buttons have the same icons and the same link states for those icons. Aside from the icon-button not having text and being a list item instead of a button proper, I don't see any difference between the two.

And yet, when I click on the icon-button, my file downloads. When I click on the icon-button-with-text, the icon state also changes like it's supposed to, but the file doesn't download. There's not even a snackbar in the corner mentioning the address of the file when I hover over the icon-button-with-text, whereas that happens when I hover over the icon-button.

Why is this happening, and what can I do so that the same file downloads from the two buttons?

Thank you in advance!

CodePudding user response:

If file's path is files\downloadableFile.pdf then in href="" set the path and in download="" set the file.

 <a href="files" download="downloadableFile.pdf">
    <img  src="icons\downloadFile_inactive.svg">
    <img  src="icons\downloadFile_active.svg">
 </a>

CodePudding user response:

You should not wrap an anchor in a button. Both elements are clickable, so behavior is not really consistent accross browsers ¹ ²

Alas, W3C's validator reports an error when nesting those elements, so it simply is not valid HTML.

Error: The element a must not appear as a descendant of the button element.

<button><a href="https://stackoverflow.com">stackoverflow</a></button>

Instead, replace your button with a div:

<div >
  <a href="files\downloadableFile.pdf" download>
    <img  src="graphics\downloadFile_inactive.svg">
    <img  src="graphics\downloadFile_active.svg">
    <span>    
      Download File
    </span>
  </a>
</div>

And of course change your CSS accordingly:

div.button1 {
  cursor: pointer;
  height: 56px;
  width: 214px;
  margin-bottom: 5%;
  border-radius: 4px;
  font-family: sans-serif;
  font-size: 16px;
  line-height: 56px;
  filter: drop-shadow(2px 2px 4px rgb(0 0 0/0.75));
}

.button1 {
  background: none;
  border: none;
  outline: 2px black solid;
  padding-left: 8.2%;
}

/* ... */
  • Related