Javascript force file download

Discussion in 'JavaScript' started by SGBoise, Jan 20, 2009.

  1. #1
    Hello,

    I writing a way to allow people to upload pdf files using my script. I want people to download the pdf file instead of opening it in their web browser.

    I google this and found that you can do it in javascript. Anyone know how to do this in javascript?
     
    SGBoise, Jan 20, 2009 IP
  2. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I just saw this in CodeIgnitor, so maybe you can check the code for what headers they are sending.
     
    NinjaWork, Jan 20, 2009 IP
    SGBoise likes this.
  3. SGBoise

    SGBoise Peon

    Messages:
    647
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks NinjaWork. That was for php code. I am looking javascript version.

    I might end up using that if I can't find a javascript code.
     
    SGBoise, Jan 20, 2009 IP
  4. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey, thanks SGBoise,

    I did a little research for you, can you add these headers and see if it works:

    
    Content-type: application/octet-stream
    Content-Disposition: attachment; filename="myfile.pdf"
    
    Code (markup):
     
    NinjaWork, Jan 20, 2009 IP
  5. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here you go...a little copy and paste ;-)

    
    <?php
    header("Content-Type:application/octet-stream");
    header("Content-Disposition:attachment;");
    $fName = basename($_GET['file']);
    fpassthru($fName);
    ?>
    
    Just save that file where you store the files on the server.
    
    If all your movies and that file were in a folder called videos, 
    and you wanted to download "random.wmv", then you'd do this:
    
    <a href="videos/filedownload.php?file=random.wmv">Cool Video!</a>
    
    Code (markup):
    from http://www.kirupa.com/forum/showthread.php?t=46853
     
    NinjaWork, Jan 20, 2009 IP
  6. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can not do this with javascript only, as the headers inside html will be read after the browser has assumed that this is a webpage.. ( and then it is too late to change them to understand it is not a page but a file for download..)
     
    gnp, Jan 20, 2009 IP
  7. W1908

    W1908 Active Member

    Messages:
    441
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Looks like you also can use this with PPI
     
    W1908, Jan 20, 2009 IP
  8. DotnetProg

    DotnetProg Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    I need to allow users to download mp3 file when they click on the link. But its opening the video in media player. but i dont want it to happen. It should open a save dialog to save the mp3 file. i need to use only Javascript.

    Any help. Its very urgent
     
    DotnetProg, Feb 9, 2011 IP
  9. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    to do this you just need to make a full href to http://yoursite.com/yourfilepath/yourfile with target='_blank' in your <a href='http://yoursite.com/yourfilepath/yourfile.mp3' target='_blank'>download</a>
     
    srisen2, Feb 14, 2011 IP