Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: Forcing Download


Message #1 by "Hiro Shiratori" <hiro-shiratori@m...> on Mon, 28 Jan 2002 11:48:31 -0800
Hi, everyone:



I'm trying to open a download dialog box on the browser.

The folloiwng should force the specific file extention (.pdf) to be 

downloaded instead of opening.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<%

 Response.Buffer =3D True

Dim strFilePath, strFileSize, strFileName

Const adTypeBinary =3D 1

strFilePath =3D Request.QueryString("File")

strFileSize =3D Request.QueryString("Size")

strFileName =3D Request.QueryString("Name")

Response.Clear

Set objStream =3D Server.CreateObject("ADODB.Stream")

objStream.Open

objStream.Type =3D adTypeBinary

objStream.LoadFromFile strFilePath

strFileType =3D lcase(Right(strFileName, 4))

    Select Case strFileType

        Case ".pdf"

   ContentType =3D "text/pdf"

    End Select

 Response.AddHeader "Content-Disposition", "attachment; filename=3D000" 

& strFileName

 Response.AddHeader "Content-Length", strFileSize

 Response.Charset =3D "UTF-8"

 Response.ContentType =3D ContentType

 Response.BinaryWrite objStream.Read

 Response.Flush

objStream.Close

Set objStream =3D Nothing

%>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



However, other files (.asp, .htm, txt & etc.) on the current folder are 

displayed from the following codes.

Does anyone know how to fix this problem?



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<%

  Dim strThisPage

 strThisPage =3D Request.ServerVariables("SCRIPT_NAME")

 strThisPage =3D Right(strThisPage, Len(strThisPage) - 1)



 FILE_FOLDER =3D 

StripFileName(Request.ServerVariables("PATH_TRANSLATED"))

%>

<HTML>

<HEAD>

 <TITLE>File Download List For</TITLE><%=3D Date() %>

 <STYLE TYPE=3D"TEXT/css">

 .TabHeader { Font-Family: Arial; Font-Weight; Bold; Font-Size: 16px; 

Background: Silver }

 .DataCol { Font-Family: Verdana; Font-Size: 12px }

 </STYLE>

 <SCRIPT>

  function msg() {

   self.status =3D 'File Downloads For <%=3D Date() %>';

   return true

  }

 </SCRIPT>

</HEAD>



<BODY onLoad=3D"msg()">

<TABLE BORDER=3D1 ID=3DtblFileData BACKGROUND=3D"">

 <TR>

  <TD CLASS=3DTabHeader>File Name</TD>

  <TD CLASS=3DTabHeader>File Type</TD>

  <TD CLASS=3DTabHeader>File Size</TD>

 </TR>

<% 

 GetAllFiles

%>



</TABLE>

</BODY>

</HTML>

<% 



Sub GetAllFiles()

 Dim oFS, oFolder, oFile

 Set oFS =3D Server.CreateObject("Scripting.FileSystemObject")

 

 Set oFolder =3D oFS.getFolder(FILE_FOLDER)



 Dim intCounter

 Dim FileArray()



 intCounter =3D 0

 ReDim Preserve FileArray(oFolder.Files.Count, 5)



 For Each oFile in oFolder.Files

  strFileName =3D oFile.Name

  strFileType =3D oFile.Type

  strFileSize =3D oFile.Size

  strFilePath =3D oFile.Path 

   

  FileArray(intCounter, 0) =3D strFileName

  FileArray(intCounter, 1) =3D "<A HREF=3D" & Chr(34) & 

"ForceDownload.asp?File=3D" _

   & strFilePath & "&Name=3D" & strFileName & "&Size=3D" & strFileSize & 

Chr(34) _

   & " onMouseOver=3D" & Chr(34) & "self.status=3D'" & strFileName & "'; 

return true;" & Chr(34) _

   & " onMouseOut=3D" & Chr(34) & "self.status=3D''; return true;" & 

Chr(34) & ">" & strFileName & "</A>"

  FileArray(intCounter, 2) =3D strFileType

  FileArray(intCounter, 3) =3D strFileSize



  intCounter =3D (intCounter + 1)

 Next



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

 

 intRows =3D uBound(FileArray, 1)

 intCols =3D uBound(FileArray, 2)



 For x =3D 0 To intRows -1

  Echo("<TR>")

  For z =3D 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 =3D Response.Write(str & vbCrLf)

End Function



Function EchoB(str)

 EchoB =3D Response.Write(str & "<BR>" & vbCrLf)

End Function



Sub Cleanup(obj)

 IF isObject(obj) Then

  Set obj =3D Nothing

 End IF

End Sub



Function StripFileName(strFile)

 StripFileName =3D Left(strFile, inStrRev(strFile, "\"))

End Function



Sub BuildTableCol(strData)

 Echo("<TD CLASS=3DDataCol>" & strData & "</TD>")

End Sub



Sub BuildTableRow(arrData)

 Dim intCols

 intCols =3D uBound(arrData)

 For y =3D 0 To intCols

  Echo("<TD CLASS=3DDataCol>" & arrData(y) & "</TD>")

 Next

End Sub



%>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




  Return to Index