1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Download (by default) a web document

Discussion in 'HTML & Website Design' started by david_sakh, Jan 28, 2005.

  1. #1
    How do I modify an anchor to allow the user by default to download an HTML doc, etc, instead of opening it with the browser?
     
    david_sakh, Jan 28, 2005 IP
  2. mopacfan

    mopacfan Peon

    Messages:
    3,273
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't have the code with me, but if no one has answered by tomorrow, I'll dig through my code library at home and find the syntax for you.
     
    mopacfan, Jan 28, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The type attribute in <a> is advisory for browsers to skip links with content types they don't support. This attribute doesn't define content type of the linked document.

    You can do this on the server side, though. In your server-side code set the content-type HTTP header to application/octet-stream.

    J.D.
     
    J.D., Jan 28, 2005 IP
  4. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #4
    but how would i make this apply to one one link on the page?

    If it's too much of a hassle I suppose I can just zip it.
     
    david_sakh, Jan 28, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The link would point to your app file (php, asp, etc) and at the beginning of the file you would need to add a line setting the response content type to the value I quoted before. The rest of the file will be plain HTML. The file will be saved by the browser as-is (minus the server-side code).

    J.D.
     
    J.D., Jan 28, 2005 IP
  6. relaxzoolander

    relaxzoolander Peon

    Messages:
    141
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #6
    heres a solution i found using asp:
    it involves 2 asp pages.
    i have not tried this script.
    if you do...please let us know the results.


    The first script will list all files available for dowload by searching the folder where they are stored:


    PAGE 1:

    First, create a new page and give it an .asp extension.
    Then design the page how you want it to look (ie, add tables, text, graphics, etc.).
    Other than the .asp extension, you can name this page whatever you like.

    Then within the html code, add the following to the very top of your page (before the opening html tag):

    <%
    Dim strThisPage
    strThisPage = Request.ServerVariables("SCRIPT_NAME")
    strThisPage = Right(strThisPage, Len(strThisPage) - 1)

    '****************************************************
    ' Add the path to the folder that holds your files in the next statement.
    '****************************************************

    Dim FILE_FOLDER
    'Add the path to the folder that holds your files here
    FILE_FOLDER = Server.MapPath("./yourfilefolder/") '<----
    %>



    Now add the following into your html code where you want the actual file names listed (eg, in a table cell):

    <%
    GetAllFiles
    %>



    Add the following to the very end of your html code (after the closing html tag):

    <%
    Sub GetAllFiles()
    Dim oFS, oFolder, oFile
    Set oFS = Server.CreateObject("Scripting.FileSystemObject")

    'Set Folder Object To Proper File Directory
    Set oFolder = oFS.getFolder(FILE_FOLDER)

    Dim intCounter
    Dim FileArray()

    intCounter = 0
    ReDim Preserve FileArray(oFolder.Files.Count, 4)

    For Each oFile in oFolder.Files
    strFileName = oFile.Name
    strFileType = oFile.Type
    strFileSize = oFile.Size
    strFilePath = oFile.Path
    strFileDtMod = oFile.DateLastModified

    FileArray(intCounter, 0) = strFileName
    FileArray(intCounter, 1) = "<A class=File HREF=" & Chr(34) & "startDownload.asp?File=" _
    & strFilePath & "&Name=" & strFileName & "&Size=" & strFileSize & Chr(34) _
    & " onMouseOver=" & Chr(34) & "self.status='" & strFileName & "'; return true;" & Chr(34) _
    & " onMouseOut=" & Chr(34) & "self.status=''; return true;" & Chr(34) & ">" & strFileName & "</A>"
    FileArray(intCounter, 2) = strFileType
    FileArray(intCounter, 3) = strFileSize
    'FileArray(intCounter, 4) = strFilePath
    FileArray(intCounter, 4) = strFileDtMod

    intCounter = (intCounter + 1)
    Next

    ' EchoB("<B>" & oFolder.Files.Count & " Files Available for Download</B>")

    intRows = uBound(FileArray, 1)
    intCols = uBound(FileArray, 2)

    For x = 0 To intRows -1
    Echo("<TR>")
    For z = 0 To intCols
    If z > 0 Then
    BuildTableCol(FileArray(x, z))
    End IF
    Next
    Echo("</TR>")
    Next


    Cleanup oFile
    Cleanup oFolder
    Cleanup oFS
    End Sub

    Function Echo(str)
    Echo = Response.Write(str & vbCrLf)
    End Function

    Function EchoB(str)
    EchoB = Response.Write(str & "<BR>" & vbCrLf)
    End Function

    Sub Cleanup(obj)
    IF isObject(obj) Then
    Set obj = Nothing
    End IF
    End Sub

    Function StripFileName(strFile)
    StripFileName = Left(strFile, inStrRev(strFile, "\"))
    End Function

    Sub BuildTableCol(strData)
    Echo("<TD CLASS=DataCol>" & strData & "</TD>")
    End Sub

    'Not implemented
    Sub BuildTableRow(arrData)
    Dim intCols
    intCols = uBound(arrData)
    For y = 0 To intCols
    Echo("<TD CLASS=DataCol>" & arrData(y) & "</TD>")
    Next
    End Sub
    %>


    Finally, save this page and link to it from your other pages.









    PAGE 2:

    Save the following code as a new .asp page and name it "startDownload.asp".

    <%
    '****************************************************
    ' This page forces the 'save as file' prompt to prevent files from being opened in the browser.
    ' It MUST be saved as "startDownload.asp" to work with the first script.
    '****************************************************

    Response.Buffer = True
    Dim strFilePath, strFileSize, strFileName

    Const adTypeBinary = 1

    strFilePath = Request.QueryString("File")
    strFileSize = Request.QueryString("Size")
    strFileName = Request.QueryString("Name")

    Response.Clear

    '*******************************
    ' Requires MDAC 2.5 to be stable
    ' I recommend MDAC 2.6 or 2.7
    '*******************************
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile strFilePath

    strFileType = lcase(Right(strFileName, 4))

    ' Feel Free to Add Your Own Content-Types Here
    Select Case strFileType
    Case ".asf"
    ContentType = "video/x-ms-asf"
    Case ".avi"
    ContentType = "video/avi"
    Case ".doc"
    ContentType = "application/msword"
    Case ".zip"
    ContentType = "application/zip"
    Case ".xls"
    ContentType = "application/vnd.ms-excel"
    Case ".gif"
    ContentType = "image/gif"
    Case ".jpg", "jpeg"
    ContentType = "image/jpeg"
    Case ".wav"
    ContentType = "audio/wav"
    Case ".mp3"
    ContentType = "audio/mpeg3"
    Case ".mpg", "mpeg"
    ContentType = "video/mpeg"
    Case ".rtf"
    ContentType = "application/rtf"
    Case ".htm", "html"
    ContentType = "text/html"
    Case ".asp"
    ContentType = "text/asp"
    Case Else
    'Handle All Other Files
    ContentType = "application/octet-stream"
    End Select

    Response.AddHeader "Content-Disposition", "attachment; filename=000" & strFileName
    Response.AddHeader "Content-Length", strFileSize
    ' In a Perfect World, Your Client would also have UTF-8 as the default in their browser
    Response.Charset = "UTF-8"
    Response.ContentType = ContentType

    Response.BinaryWrite objStream.Read
    Response.Flush

    objStream.Close
    Set objStream = Nothing

    %>

    For these two scripts to work properly they must be stored in the same file folder. The first script can be embedded in any page you like using any file name you prefer (eg, myfiles.asp). The second script MUST be saved as "startDownload.asp" since the first script specifically calls that page. Then just link to the page that contains the first script (eg, "myfiles.asp) from your other web pages. When the first page (eg, "myfiles.asp") is accessed, the script will display and sort the files in alphabetical order. When a file name on this page is clicked, the browser will open the "save as file" prompt. That's it!

    Since I see this question a lot, I offer the following two asp scripts that I discovered some time ago (I don't remember where). I've used them since with absolutely no problems, and with a little tweaking (if it's even necessary), you should be able to make them work for you. These scripts will force the download prompt to appear instead of opening the file in the browser, but only when the content-type is pre-defined in the second script. Undefined content-types will still open in the browser.

    :)
     
    relaxzoolander, Jan 28, 2005 IP
  7. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That's a fancy way of reading a file and returning it to the browser. Here's a simpler version:

    <%@ language="JScript" %>
    <% Response.ContentType = "application/octet-stream";
       Response.AddHeader("Content-Disposition", "attachment; filename=\"html-file.html\""); 
    %>
    <html>
    <head><title>Test HTML File</title></head>
    <body>
    <p>This is a plain HTML file</p>
    </body>
    </html>
    Code (markup):
    Save this as an ASP file and when you hit it with the browser, it will prompt you to save the result as html-file.html. Content-Disposition is not a part of HTTP (it's an email thing) and is considered as a potential security threat. With this in mind, you can remove the entire AddHeader line (the result will be that the file will be saved as an ASP file).

    J.D.
     
    J.D., Jan 29, 2005 IP