Jump to content

downloadable mp3 files


Recommended Posts

Hello Team,

I have a podcast application, where a user  can download mp3 file.

I have given a simple link to mp3 file in the editor, but it never downloads. It opens in the browser.

Is there any setting in pw cms that is preventing this behaviour.

Let me know. Thanks.

 

 

Link to comment
Share on other sites

10 hours ago, shivam224 said:

I have given a simple link to mp3 file in the editor, but it never downloads. It opens in the browser.

Is there any setting in pw cms that is preventing this behaviour.

This isn't caused by ProcessWire; it's just the default behaviour of browsers. The same as if you followed a link to a JPG file the content would be shown in the browser rather than downloaded.

The simplest way to have any link trigger a download is to add the "download" attribute to the <a> tag: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download

If you were constructing the link markup in PHP by looping over the files in a Files field it would be easy to add that download attribute to the link. But it sounds like you are creating the links in CKEditor and it's a bit more difficult there because both ACF and HTML Purifier will strip out the download attribute by default.

2021-07-20_102741.png.e842f481ea3a7746735eb61a836fa380.png

Rather than messing around trying to configure ACF and HTML Purifier to allow the download attribute I would simply add the attribute on the front-end with a bit of JavaScript. Here's how you could automatically add the download attribute to any MP3 link using jQuery:

$(document).ready(function() {
    // Add "download" attribute to MP3 links
    $('a[href$=".mp3"]').attr('download', '');
});

 

  • Like 2
Link to comment
Share on other sites

Thanks for reply.

If I give a mp3 link from a normal html file (outside of processwire), it downloads.

But from pw cms it doesn't download.

I will try option suggested by you.

 

 

Link to comment
Share on other sites

If I had to guess what's happening here, I'd say that when you supply the file from your normal html, the server may not be sending an content-type header so the browser downloads it, as the server hasn't told it what type of data it is. When PW sends the data, it's probably setting the content-type header - and the browser, now knowing what it is, takes action to play it.

Link to comment
Share on other sites

yes  netcarver, I guess what you said may be happening in case of normal html.

But where and how do I specify that mp3 file to be downloaded in Pw . In my application there are 2 options, one is to play the mp3 file (which works in pw using module) and the other is to download that mp3 file (which doesn't work in pw)

Thanks.

Link to comment
Share on other sites

Are you using pagefileSecure? If you do, or otherwise use ProcessWire to send the file, you can change the config to force mp3s to always download using this incantation:

$config->fileContentTypes = array_merge($config->fileContentTypes, ['mp3' => '+audio/mpeg']);

The + sign in front of the mime type will tell PW to use the header “Content-Disposition: attachment” when sending the file type in question. Cf. https://github.com/processwire/processwire/blob/master/wire/config.php#L632

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...