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

Hybrid View

  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
    May 2013
    Posts
    1
    Here's how you make download link for MP3 files.
    change "octet/stream" to "audio/mp3"

  8. #8
    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

  9. #9
    Join Date
    Sep 2013
    Posts
    2

    Please help

    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'];
    
    echo"$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


    when I use it in my website and browse from my iPhone then a unknown result is shown such as " Vfv7GWgw5!1AQaq"2B#R3$brCScs4%& 5DTdEU6teuFVfv'7GWgw ?:{\.8T߀]aa]vVmwX74 4HlNnoOЉۯ`?94dm[Uk h:w@99UF yC_G9-} \K*,qԗ:9iy>u _} "u2-'eU]EKlI:ۉV-.>;S> h`N+*8x*W\>j`e5.pCTnw4rnnP?]qz>Cqr{p u6Ei,p?+k*Ch $'nQ\]wjds?"*m-|'[>auɟM0qu'z2744` ]eXC\H!V{WE]v#l6zk Y춶1**Yә~Ks_}eEG{?G?2UIV+u[~uQNAeie$`_~kAiC5t;ȳ/ͫXY gnڪ5O\GGu}F? ^?῜co@/&f3VH$4.w{wSOw z+h\CegnvcSsf*sm/*XYXk]׵>^+ @*V٘?Υ4GT;0gN$ѦOʏ/ʵ"[׺Z46*r=yY*uٲ\ymU)0:GCVZzWgN{>w__qGܓQ}wC @9*L;-;&Ϻ*>]Cw*tCS%vCԩG*1s6ɘEqN^) Z)˾eL+ wI˪8,6ӑ43.........." like this.
    please help me

  10. #10
    Join Date
    Apr 2014
    Posts
    1
    Hi Eye for Video. Thanks for this solution which i just found on the net. I want people to download music from my site. This works on my desktop but I got someone to test it on their mobile and it just downloads the php file. Do you know is it suppose to work on mobile? Thanks a lot
    Sarah

  11. #11
    Join Date
    Apr 2014
    Posts
    1

    Unhappy

    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
    Hi,

    I tried the above but when I call the .mp3 file I receive the following error message:

    Error 500: (Internal server error)

    Any thoughts?

  12. #12
    Join Date
    Apr 2013
    Location
    http://www.nile7.com/services
    Posts
    1
    Quote Originally Posted by MrSnowDrop View Post
    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.
    yes , i'm wondering too

  13. #13
    Join Date
    Apr 2013
    Posts
    1

    help pls

    <?php
    //Force file download instead of streaming
    //Coded by Code018


    $file=$_GET['file'];
    $file=str_replace(" ","%20",$file);
    header('Content-type: application/octet-stream');

    // It will be called the base file name
    $url = $file;
    $file_name = basename($url);

    //lets be nice to the user and replace the spaces with happy things
    //$file_name=str_replace("%20","_",$file_name);

    //this is the filename we get to play with
    $infile = $url;
    $file_name = stristr ($infile,basename ($infile));
    header('Content-Disposition: attachment; filename="'.urldecode($song_name.mp3.'"');

    // The File source is in .mp3 originally
    //This is the file that we are downloading
    readfile(stripslashes($file));


    ?>

    when add .mp3 file download but not actual
    pls reply fast

  14. #14
    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.

  15. #15
    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'


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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