[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1782
  • Last Modified:

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
0
MRSONNY
Asked:
MRSONNY
  • 6
1 Solution
 
GaryCommented:
<%
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
 
MRSONNYAuthor Commented:
Wonderfully !

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

0
 
MRSONNYAuthor Commented:
File size wrong i guess !
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MRSONNYAuthor Commented:
<%
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
 
fritz_the_blankCommented:
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
 
MRSONNYAuthor Commented:
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
 
MRSONNYAuthor Commented:
When the Fil size get to big, the file wont come down at all !
0
 
MRSONNYAuthor Commented:
Not enough storage is available to complete this operation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now