Link to home
Start Free TrialLog in
Avatar of MRSONNY
MRSONNY

asked on

Download a file for a local drive (from WWW)

Hi !

I'm using FileSystemObject to work with files on the server, but when it comes to download a
file to the client then..... mm.

What I have :
File on drive e:\files\blabla\test.doc

What I want :
When the client click on a object/link on the web site, the file from 'e:\files\blabla\test.doc'
should be downloaded to the client.

The solution:
?

regards
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MRSONNY
MRSONNY

ASKER

Wonderfully !

It comes down alright, but the file becomes korrupt.
MM..

Avatar of MRSONNY

ASKER

File size wrong i guess !
Avatar of MRSONNY

ASKER

<%
strFileName=request.QueryString("fil")'"ee1.txt" ' Change to filename
strFilePath=(session("VimgFolder") & request.QueryString("fil"))'server.mappath(strFileName)
strFileSize=request.QueryString("Size") 'Getting file Size

sFileType= Right(strfileName,4)

' Show me Variable for Err track
response.Write("<br>Path     = " & strFilePath)
response.Write("<br>Name     = " & strFileName)
response.Write("<br>File Type = " & sFileType)
response.Write("<br>File Size = " & strFileSize)
'response.End()

Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select

Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
'Const adTypeBinary = 1

objStream.Type = 1 'adTypeBinary
strFileType = sContentType
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = sContentType
Response.BinaryWrite strFilePath
Response.Flush
objStream.Close
Set objStream = Nothing


%>
The same kind of thing here--just change the path part to match your environment:


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
Function downloadFile( strFile, strDownloadFilename )
     Dim strFilename,objStream,objFilesystem,objFilestream
     Dim intFileLength
     ' get full path of specified file
     strFilename = Server.MapPath( "../Uploads/temp/"  & strFile)
     ' clear the buffer
     Response.Buffer = True
     Response.Clear

     ' create stream
     Set objStream = Server.CreateObject("ADODB.Stream")
     objStream.Open

     ' set as binary
     objStream.Type = 1

     ' check the file exists
     Set objFilesystem = Server.CreateObject("Scripting.FileSystemObject")
     if not objFilesystem.FileExists(strFilename) then
          Response.Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
          Response.End
     end if


     ' get length of file
     Set objFilestream = objFilesystem.GetFile( strFilename )
     intFilelength = objFilestream.size
 
     objStream.LoadFromFile( strFilename )
     if err then
          Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
          Response.End
     end if
     
     'format strFileName
     if Len( Trim(strDownloadFilename) ) > 0 then
          strDownloadFilename = Trim( strDownloadFilename )
     else
          strDownloadFilename = objFilestream.name
     end if

     ' send the headers to the users browser
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strDownloadFilename
     Response.AddHeader "Content-Length", intFilelength
     Response.Charset = "UTF-8"
     Response.ContentType = "application/octet-stream"

     ' output the file to the browser
     Response.BinaryWrite objStream.Read
     Response.Flush

     ' tidy up
     objFilestream.Close
     Set objFilestream = Nothing

End Function
%>
</HEAD>
<BODY>
<%
Call downloadFile( Replace( Request("FILE") ,"/","\"), Request("FILENAME") )
'Response.Write (Request("FILE")  & "----" & Request("FILENAME") )
%>

</BODY>
</HTML>


BTW--I stole this code from someone else, but can't find the reference.

FtB
Avatar of MRSONNY

ASKER

WEll , resloved it this way :



<%
strFileName=request.QueryString("fil")'"ee1.txt" ' Change to filename
strFilePath=(session("VimgFolder") & request.QueryString("fil"))'server.mappath(strFileName)
strFilePath = replace(strFilePath,"//","/")
strFileSize=request.QueryString("Size") 'Getting file Size

sFileType= Right(strfileName,4)


Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select

' Show me Variable for Err track
response.Write("<br>Path     = " & strFilePath)
response.Write("<br>Name     = " & strFileName)
response.Write("<br>File Type = " & sFileType)
response.Write("<br>File Size = " & strFileSize)
response.Write("<br>File type = " & sContentType)
'response.End()



'Create a stream object
Response.Clear
  Dim objStream
  Set objStream = Server.CreateObject("ADODB.Stream")
 
  'Open a GIF file
  objStream.Type = adTypeBinary
  objStream.Open
  objStream.LoadFromFile strFilePath
 
  'Output the contents of the stream object
  Response.Charset = "UTF-8"
  Response.ContentType = sContentType
  Response.BinaryWrite objStream.Read
 
  ' ***********  Send bilde til browser  *********
  Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
strFileType = sContentType
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = sContentType
Response.BinaryWrite strFilePath
Response.Flush
objStream.Close
Set objStream = Nothing
  ' *********************
 
  'Clean up....
  objStream.Close
  Set objStream = Nothing



%>
Avatar of MRSONNY

ASKER

When the Fil size get to big, the file wont come down at all !
Avatar of MRSONNY

ASKER

Not enough storage is available to complete this operation.