Force Download Code

Discussion in 'C#' started by OnlineD, Nov 25, 2007.

  1. #1
    I have placed some code on a page that forces a download. I have it working if I hard code the filename (as shown below), however in the live application I need to populate the filename from a database. Can anyne tell me what is the coreect syntax to correctly enter the variable filename into this code - I think I keep getting the ," and ) in the wrong place.

    <!-- Start of Force Download Code -->
    <%
    Response.Buffer=true
    On Error Resume Next
    'Create a stream object
    Dim tfm_downloadStream
    Set tfm_downloadStream = Server.CreateObject("ADODB.Stream")
    tfm_downloadStream.Type = 1
    tfm_downloadStream.Open
    tfm_downloadStream.LoadFromFile Server.Mappath("../secure_documents/DavidsPDF_2_1.pdf")
    If Err.number = 0 Then
    Response.Clear
    Response.ContentType = "application/octet-stream"
    Response.AddHeader "Content-Disposition", "attachment; filename=DavidsPDF_2_1.pdf"
    Response.AddHeader "Content-Transfer-Encoding","binary"
    Response.BinaryWrite tfm_downloadStream.Read
    tfm_downloadStream.Close
    Set tfm_downloadStream = Nothing
    Response.End()
    Else
    tfm_downloadStream.Close
    Set tfm_downloadStream = Nothing
    Response.Redirect("document_error.asp")
    End If
    %>
    <!-- End of Force Download Code -->

    The variable fielname code is (rsDocument.Fields.Item("DocumentFilename").Value)

    Many thanks.
     
    OnlineD, Nov 25, 2007 IP
  2. ashrafweb

    ashrafweb Member

    Messages:
    67
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    prev_code..
    MyFileName = rsDocument.Fields.Item("DocumentFilename").Value
    MyFileName = "("../secure_documents/" + MyFileName
    tfm_downloadStream.LoadFromFile Server.Mappath(MyFileName)
    next_code...
     
    ashrafweb, Nov 26, 2007 IP