Making a file downloable that a browser doesn't try to open

Discussion in 'HTML & Website Design' started by kevin_laurentian, Sep 19, 2010.

  1. #1
    Hey everyone,

    I've got a few files I wanted available for download for my friends site. And for some reason I can't get it to download once I click on the link?

    <a href="brochures.rar" target="_blank">Download Brochures</a></div>

    Everytime i click on the download brochures link, the browser opens the file and its a bunch of code and symbols. I just want a pop up just like every other downloadable file on the net.

    Any help would be appreciated.
     
    kevin_laurentian, Sep 19, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It may be set to automatically open in your browser. If you are using Firefox, look in Tools->Options->Applications for the WinRAR content types.
    Or you can select a RAR file in Windows Explorer, right click and pick Open With->Choose Program, and then uncheck the "Always open..." box.
     
    Cash Nebula, Sep 19, 2010 IP
  3. WWSD

    WWSD Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The content type header may be being sent incorrectly.
     
    WWSD, Sep 19, 2010 IP
  4. kevin_laurentian

    kevin_laurentian Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What exactly do you mean that the content type header may be being sent incorrectly, - how to fix this issue if indeed it is the issue.

    Thanks.
     
    kevin_laurentian, Sep 24, 2010 IP
  5. kevin_laurentian

    kevin_laurentian Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    well - how come other websites that have rar files available for download come up with a pop up download box when i download them but the one on the site im working on doesn't? Something wrong with my code?
     
    kevin_laurentian, Sep 24, 2010 IP
  6. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In that case, the server is probably at fault.

    Did you try removing target="_blank"? It didn't cause a problem in my browser, but you might be using something different.
     
    Cash Nebula, Sep 24, 2010 IP
  7. dementic

    dementic Peon

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    well, since you didnt give a link,
    this is general info.

    if your site is a php site,
    then you can create a php file
    and add:
    <?php
    header('Content-disposition: attachment; filename=filename.rar');
    header('Content-type: octet-stream);
    readfile('filename.rar');
    ?>
    for example. this will force the download of the file ( please read more about content-types to apply it to your situation)

    if your link is a simple html link, you can use javascript to force download it:
    <a href="javascript:void(0);"
    onclick="document.execCommand('SaveAs',true,'filename.rar');"
    >Download</a>

    if it is .net then create a download.aspx file and add this (VB EXAMPLE)
    <%@ Page language="vb" runat="server" explicit="true" strict="true" %>
    <script language="vb" runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs)
    Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring
    If strRequest <> "" Then 'get absolute path of the file
    Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo
    Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
    If file.Exists Then 'set appropriate headers
    Response.Clear()
    Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
    Response.AddHeader("Content-Length", file.Length.ToString())
    Response.ContentType = "application/octet-stream"
    Response.WriteFile(file.FullName)
    Response.End 'if file does not exist
    Else
    Response.Write("This file does not exist.")
    End If 'nothing in the URL as HTTP GET
    Else
    Response.Write("Please provide a file to download.")
    End If
    End Sub
    </script>

    hope that helps...
    next time, please give example and link ;)
     
    Last edited: Sep 25, 2010
    dementic, Sep 25, 2010 IP
  8. Aresm

    Aresm Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Lol thanks for that bro
     
    Aresm, Sep 25, 2010 IP