Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    GA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing Save As Filename

    Hello. I am new to the forums (I just started my Javascript journey last week), and was hoping to get help with something. I am currently developing a page which allows access to music on a server. This music is being streamed, but users are also able to download this music (assuming they have the license to do so). All of the music uploaded is of the .mp3 format, however they have no filetype extensions, and were not named using any particular naming convention. Because of this, I was hoping to change the filename (when a downloaded is initiated) to variables I have stored based on the current song.

    I am currently simply opening a new page, in Chrome is looks like this:
    Code:
    chrome.tabs.create({ "url": song.url })
    Hit presents the user with a prompt to save the file, however, its default name is a random alphanumeric string with no extension, as per their file name on my server. Is there an easy way to do this without renaming all the music server side? I do have the variables song.title and song.artist setup!

    All help is appreciated!

    Edit: Since this page is only for Chrome users, I listed the code I am using. I understand window.open and was originally using this. It functions the same, and if I need to revert back to using is instead, I shall.
    Last edited by Lateralshift; 12-20-2010 at 06:36 PM.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You would want to do this on the server side (probably with PHP).

    What you need to do is set up a database (probably MySQL) to store the real desired "display" filename along side the filename present on your server for that song. So, very generally speaking, your database would be something like this:

    Code:
    Filename		Display Name
    djhsad8asdfaisd89fa	The Beatles - Yellow Submarine.mp3
    932nqf8adnfas8b32f	Metallica - Enter Sandman.mp3
    ...
    The link to download a track would send them to a PHP handler page that selects the file to send and sets the file information sent to the user. The link would look something like this:

    Code:
    http://www.yoursite.com/download.php?track=n38afnbsf8aefawhnas83b
    And the value of "track" would just be the alphanumeric string that your file is stored under.

    Then, when a user chooses to download a track, the server-side code (PHP) checks to make sure the file exists and then checks the database for the "display name" you want to use for that file. It then prepares to send the file using document header text to set the filename of the "save me" file as the display filename rather than the actual filename and it then sends the file contents to the client user.

    It wouldn't be incredibly difficult to do if you have some PHP familiarity. Look into MySQL databases and the following PHP functions:

    mysql_query()
    mysql_fetch_array()
    header() (and pay attention for the "Content-Disposition:" header, this is where you can set the filename that is suggested to the client user)


    As long as your host provides PHP and MySQL access this is the way to go. You can even store all of your song tracks in a directory not directly accessible ("above" the "www" folder or the "public_html" folder as seen in most hosting environments) and still serve them to authenicated users this way.

    Anyway, I don't want to make your question broader than you meant it to be. The bottom line is that this is best accomplished with server-side scripting, ESPECIALLY if you want to prevent unauthenticated users from accessing your files.



    Other than that, if you're planning on using only javascript you would have to have an array of song titles and filenames built into the script - which every user could see and potentially abuse.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Location
    GA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot for the quick reply! I had looked into PHP handlers but was thinking it would be more simple if I could simply use the values already sitting in my code.

    I will definitely do some research on this tonight and post here if I have anymore questions. Also, is there an easy way to add .mp3 to the end of the filename without PHP/SQL

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by Lateralshift View Post
    Thanks a lot for the quick reply! I had looked into PHP handlers but was thinking it would be more simple if I could simply use the values already sitting in my code.

    I will definitely do some research on this tonight and post here if I have anymore questions. Also, is there an easy way to add .mp3 to the end of the filename without PHP/SQL

    all you need to do is use php to set the content-disposition header to reflect your display name + ".mp3".


    PHP example:

    Code:
    header('Content-Disposition: attachment; filename=' . $pretty . '.mp3' );
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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