www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: [RESOLVED] Force download of MP3 file instead of streaming

  1. #1
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    resolved [RESOLVED] Force download of MP3 file instead of streaming

    Hi, i had encountered problem with MP3 file links:
    in IE/Safari/Chrome, when a link to a MP3 file is clicked, the download starts correctly; but in Firefox that doesn't happen - clicking the MP3 file link, it starts an unwanted streaming through a preinstalled FF media plugin.
    I need to force MP3 file download in all browsers, is there a simple way to do that in Javascript?

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Not really in javascript, but if you set the files mimetype to application/octet-stream you would normally receive the dialog(since that's a universal binary/cannot display in the browser but not saying what file type it is yet mime)
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    MIME types

    Quote Originally Posted by scragar View Post
    Not really in javascript, but if you set the files mimetype to application/octet-stream you would normally receive the dialog(since that's a universal binary/cannot display in the browser but not saying what file type it is yet mime)
    I'm using HTML/VBscript/Javascript (not PHP) in my webpages, but i thought to open a Multimedia Thread because the problem is an unwanted streaming process.

    So, how is it possible to change a MIME attribute of an MP3 file?... The need is to leave the file with his original extension because the same file is managed by a web-mediaplayer, but i'd like to let a download by clicking a specified button.

  4. #4
    Join Date
    Feb 2008
    Location
    NW Washington State
    Posts
    1,856
    Another option is to use a small PHP file to handle the download, put in the same folder as the .mp3:
    Code:
    <?php
    $file = $_GET['file'];
    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename=".$file.";");
    header("Content-Length: ".filesize($file));
    readfile($file);
    exit;
    ?>
    which can be accessed in any anchor like this:
    Code:
    <a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
    see sample page here:
    http://www.cidigitalmedia.com/tutori..._download.html
    Best of luck,
    Eye for Video
    www.cidigitalmedia.com

  5. #5
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    php solution

    Thanks EyeForVideo, it seems PHP is the only way to resolve the problem... Unfortunately my pages are ASP extensions and dialog with my Database through VBscript code, so PHP is a little uncomfortable; that's the reason why i hope in a kind of Javascript solution
    I've to learn PHP and create a dynamic page which redirects to MP3 files (download.php?fileID=1)... At the moment, i have to admit it, have really no idea in how to build dynamic php code.

    Thanks again, regards.

  6. #6
    Join Date
    Feb 2008
    Location
    NW Washington State
    Posts
    1,856
    Are you sure that it has to be that complex?
    You might want to at least give it a try.
    Just create a simple PHP file from the code above. Name it
    direct_download.php
    It is NOT a part of any Web page, it is a small separate file, it just needs to reside in the same folder as the mp3.
    The <a> just needs to use that file as the href.
    It's just a plain anchor... nothing special, no different than linking to any other web page or file.
    Best of luck,
    Eye for Video

  7. #7
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    It's possible in asp.

    Code:
    <%
    Response.ContentType = "Application/octet-stream";
    string FilePath = MapPath("song.mp3");
    Response.WriteFile(FilePath)
    Response.End()
    %>
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  8. #8
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    Dynamic php

    @EyeForVideo - Sure, i'll give a try next days... the other thing to consider, is that there's not a single file, but i have to manage a lot of MP3 through a Database, so i have to make the php-content dynamic: the php page has to request an ID number, corresponding to a MP3 file in the DB... i'll study some php to do that (i think is a little different from asp/vbscript code).

    Regards

  9. #9
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    I've just had another thought, can't you just modify the iis settings?

    http://technet.microsoft.com/en-us/l.../bb742440.aspx
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  10. #10
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    errors

    Quote Originally Posted by scragar View Post
    It's possible in asp.

    Code:
    <%
    Response.ContentType = "Application/octet-stream"
    string FilePath = MapPath("song.mp3")
    Response.WriteFile(FilePath)
    Response.End()
    %>
    I'll try again, but at the moment this is the result:

    Microsoft VBScript runtime error '800a000d'
    Type mismatch: 'mappath'


  11. #11
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    Possible solution

    Quote Originally Posted by scragar View Post
    I've just had another thought, can't you just modify the iis settings?

    http://technet.microsoft.com/en-us/l.../bb742440.aspx
    I think that could be a possible solution, although now the result is:

    in IE - nothings changed (but it's ok, with IE i had no problems in download <a href="song.mp3">...)

    in FF - it asks me if i want to save the whole ASP page!!!

    I'll have to study hard next days, thanks a lot for the moment
    Last edited by MrSnowDrop; 03-24-2011 at 10:25 PM.

  12. #12
    Join Date
    Feb 2008
    Location
    NW Washington State
    Posts
    1,856
    the PHP part is just one simple file and can handle multiple files... here's an example:
    http://www.worldbarefootcenter.com/v...oad_video.html
    Download links for a gazzillion video.
    It's just an anchor... if you can create a dynamic anchor (forget about there being a link to the PHP file).. link to it just as if it where an image file.
    Best wishes,
    EfV

  13. #13
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101

    Resolved!

    Quote Originally Posted by Eye for Video View Post
    the PHP part is just one simple file and can handle multiple files...
    WONDERFUL, PHP mini-page works pefect!... I'm sorry, i didn't immediately understood that it meant to be a dynamic page, i thought "file" was only an example in php...
    I only have to put it in any folder where MP3 files are placed (the files are album-ordered), but really this is not a problem because the php code is very short.

    Well, finally i have to admit that PHP code has so many rich functions and i don't understand why other server-side languages (like ASP) cannot do the same things...

    Thanks a lot EyeForVideo!

  14. #14
    Join Date
    Feb 2012
    Location
    Leesburg, Va.
    Posts
    2
    Quote Originally Posted by Eye for Video View Post
    Another option is to use a small PHP file to handle the download, put in the same folder as the .mp3:
    Code:
    <?php
    $file = $_GET['file'];
    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename=".$file.";");
    header("Content-Length: ".filesize($file));
    readfile($file);
    exit;
    ?>
    which can be accessed in any anchor like this:
    Code:
    <a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
    see sample page here:
    http://www.cidigitalmedia.com/tutori..._download.html
    Best of luck,
    Eye for Video
    www.cidigitalmedia.com
    Unfortunately, using Safari, when I follow the download link using your PHP code, Safari downloads the PHP code, not the referenced MP3 file.

    Will this not work, if the page code is in a directory one level higher than the MP3 files and the PHP code?

    Yours,

    Dominik Hoffmann

  15. #15
    Join Date
    Feb 2008
    Location
    NW Washington State
    Posts
    1,856
    Are you saying that the download links I posted do not work in Safari?
    The links all work just fine in Safari on my machine.
    Or are you having problems when you implement that download php file?
    Code:
    <a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
    of course the href=" has to be the correct path. So if the web page with the link on it is one level up or down, the path has to reflect that.... but it will work just fine.
    Also the file= name has to be changed to the name of the file you are downloading. If you put file=direct_download.php, then it will download that file. So recheck your link code.
    EfV

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles