Solved

Download a file for a local drive (from WWW)

Posted on 2003-11-21
8
1,776 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 200 total points
ID: 9795777
<%
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
ID: 9795892
Wonderfully !

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

0
 

Author Comment

by:MRSONNY
ID: 9795903
File size wrong i guess !
0
Independent Software Vendors: 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!

 

Author Comment

by:MRSONNY
ID: 9796059
<%
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9796556
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
ID: 9796623
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
ID: 9949073
When the Fil size get to big, the file wont come down at all !
0
 

Author Comment

by:MRSONNY
ID: 10666928
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query still returning duplicates 5 50
ASP exit 10 26
ASP and Extracting XML 7 25
I need to Integrate Hubspot into my .asp application but don't know how to POST 11 27
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

735 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