with just a plain html and js file, how can i force a link to an mp3 to download instead of just play?

前端 未结 7 2054
误落风尘
误落风尘 2021-01-13 15:50

i have a link to a media file such as an mp3, and i want it to get downloaded when the user clicks on it, instead of just having the file get played. the page i have in min

相关标签:
7条回答
  • 2021-01-13 16:00
    <a href="linkto.mp3" download>Download MP3 File </a>
    
    0 讨论(0)
  • 2021-01-13 16:07

    If you want them to be forced to download it from a link, then add the download attribute to the link element (filename optional):

    <a href="file.mp3" download="Song_Name.mp3">Download</a>
    

    If you want users to be forced to download the file when they visit the URL by any means, you are out of luck. That is not possible with just HTML and JS. You have to have access to the server to affect the headers of the file before it gets loaded in the browser, as others have said.

    0 讨论(0)
  • 2021-01-13 16:12

    Client side JavaScript can't achieve that (the nearest functionality is document.execCommand('SaveAs'), wich just saves the entire html page).

    0 讨论(0)
  • 2021-01-13 16:17

    Agree with smazurov, and ultimately you cannot control the presentation of your media although the Content-disposition may get it done for you. Here is the RFC on the header in question.

    0 讨论(0)
  • 2021-01-13 16:18

    While this is not a solution, change the file extension to anything other than MP3. And, ask the user to rename it once downloaded.

    0 讨论(0)
  • 2021-01-13 16:21

    In order to make that happen you need to send a header with Content-disposition: attachment; filename=fname.ext header in your favorite language before sending the file.

    Without knowing the specifics such as what language and what control you have over your server configuration I cannot give you more detailed instructions. You cannot do it with just html and javascript however.

    0 讨论(0)
提交回复
热议问题