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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
File size wrong i guess !
ASKER
<%
strFileName=request.QueryS tring("fil ")'"ee1.tx t" ' Change to filename
strFilePath=(session("Vimg Folder") & request.QueryString("fil") )'server.m appath(str FileName)
strFileSize=request.QueryS tring("Siz e") '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
%>
strFileName=request.QueryS
strFilePath=(session("Vimg
strFileSize=request.QueryS
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
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,objF ilesystem, objFilestr eam
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("Scrip ting.FileS ystemObjec t")
if not objFilesystem.FileExists(s trFilename ) 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
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
Function downloadFile( strFile, strDownloadFilename )
Dim strFilename,objStream,objF
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
objStream.Open
' set as binary
objStream.Type = 1
' check the file exists
Set objFilesystem = Server.CreateObject("Scrip
if not objFilesystem.FileExists(s
Response.Write("<h1>Error<
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:
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
ASKER
WEll , resloved it this way :
<%
strFileName=request.QueryS tring("fil ")'"ee1.tx t" ' Change to filename
strFilePath=(session("Vimg Folder") & request.QueryString("fil") )'server.m appath(str FileName)
strFilePath = replace(strFilePath,"//"," /")
strFileSize=request.QueryS tring("Siz e") '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
%>
<%
strFileName=request.QueryS
strFilePath=(session("Vimg
strFilePath = replace(strFilePath,"//","
strFileSize=request.QueryS
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
'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
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
%>
ASKER
When the Fil size get to big, the file wont come down at all !
ASKER
Not enough storage is available to complete this operation.
ASKER
It comes down alright, but the file becomes korrupt.
MM..