Solved

Download a file for a local drive (from WWW)

Posted on 2003-11-21
8
1,763 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:MRSONNY
  • 6
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 200 total points
Comment Utility
<%
strFileName="ee1.txt" ' Change to filename
strFilePath=server.mappath(strFileName)

sFileType= Right(fileName,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

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

objStream.Type = 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
%>
0
 

Author Comment

by:MRSONNY
Comment Utility
Wonderfully !

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

0
 

Author Comment

by:MRSONNY
Comment Utility
File size wrong i guess !
0
 

Author Comment

by:MRSONNY
Comment Utility
<%
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


%>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 

Author Comment

by:MRSONNY
Comment Utility
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



%>
0
 

Author Comment

by:MRSONNY
Comment Utility
When the Fil size get to big, the file wont come down at all !
0
 

Author Comment

by:MRSONNY
Comment Utility
Not enough storage is available to complete this operation.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now