ABaruh
asked on
Text getting appended to file downloaded using Response.BinaryWrite
I have been using the code that follows to download files from a web application. It seems to work fine for all files except text files. It is appending a bunch of text to the end of the file I download. The text is the HTML code that is part of the Download.asp file I downloaded the file from. Here is the entire code from download.asp:
<%@Language="VBScript"%>
<%
Option Explicit
Dim PID
PID = Request.QueryString("PID")
%>
<html>
<head>
<link type="text/css" rel="stylesheet" href="../General/style.css ">
<TITLE>My Title</TITLE>
<script language="javascript">
function ReloadParent()
{
window.opener.location.hre f='ProjFil es.asp?Act n=Edit&PID =<%=PID%>' ;
}
</script>
</head>
<body leftMargin=0 rightmargin=0 topmargin=0 onLoad="clock();">
<!--#include file="../General/Globals.i nc"-->
<%
Src = "Download"
%>
<!--#include file="../General/ConnStr.i nc"-->
<!--#include file="../General/VerifyUsr Open.inc"- ->
<!--#include file="../General/Title.inc "-->
<!--#include file="../General/VerifyUsr Close.inc" -->
<%
Dim strFileLocation, strRootPath
Dim FID
FID = Request.QueryString("FID")
Dim FileName
FileName = Request.QueryString("FileN ame")
If FID <> "" Then
Dim ConnGetRootPath, SQLGetRootPath, RSGetRootPath
Set ConnGetRootPath = Server.CreateObject("ADODB .Connectio n")
ConnGetRootPath.Open ConnStr
SQLGetRootPath = "SELECT Root_Path_For_Files " &_
"FROM t_System_Information"
Set RSGetRootPath = ConnGetRootPath.Execute(SQ LGetRootPa th)
strRootPath = RSGetRootPath("Root_Path_F or_Files")
RSGetRootPath.Close
Set RSGetRootPath = Nothing
ConnGetRootPath.Close
Set ConnGetRootPath = Nothing
Dim ConnGetFileLocation, SQLGetFileLocation, RSGetFileLocation
Set ConnGetFileLocation = Server.CreateObject("ADODB .Connectio n")
ConnGetFileLocation.Open ConnStr
SQLGetFileLocation = "SELECT File_Location " &_
"FROM t_Files " &_
"WHERE [File_ID] = "& FID &""
Set RSGetFileLocation = ConnGetFileLocation.Execut e(SQLGetFi leLocation )
strFileLocation = RSGetFileLocation("File_Lo cation")
RSGetFileLocation.Close
Set RSGetFileLocation = Nothing
ConnGetFileLocation.Close
If Request("Download.x") = "" Then
' Update record in t_Files that the file is now checked out
Dim ConnCheckOut, SQLCheckOut, RSCheckOut
Set ConnCheckOut = Server.CreateObject("ADODB .Connectio n")
ConnCheckOut.Open ConnStr
SQLCheckOut = "exec usp_FileCheckInOut " &_
"@Type = 'CheckOut', " &_
"@FileID = "& FID &", " &_
"@CheckedBy = "& UID &""
Set RSCheckOut = ConnCheckOut.Execute(SQLCh eckOut)
ConnCheckOut.Close
Set ConnCheckOut = Nothing
End If
Function downloadFile( intFileID, strFilePath, strFile, strDownloadFilename )
Dim strFilename,objStream,objF ilesystem, objFilestr eam
Dim intFileLength
' get full path of specified file
strFilename = strFilePath
' 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 that the file exists
Set objFilesystem = Server.CreateObject("Scrip ting.FileS ystemObjec t")
' 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
objstream.Close
Set objstream = Nothing
End Function
If request("Download.x") <> "" Then
Call downloadFile(Request.Query String("FI D"), strRootPath & strFileLocation & Request.QueryString("FileN ame"), Request.QueryString("FileN ame"), Request.QueryString("FileN ame"))
End If
End If
%>
<form name="frmDownload" method="post" action="Download.asp?FID=< %=FID%>&Fi leName=<%= FileName%> " style="display:inline;">
<table width="100%" align="center">
<tr valign="center">
<td align="center">
<br><br><br>
Your file is ready to download. Click here to start
<br><br>
<input type="image" name="Download" src="../Images/DownloadBtn .jpg" value="DOwnload" alt="Download File" align="absmiddle" onclick="ReloadParent();">
<br><br><br><br><br>
Click <a href=javascript:self.close ()><span class="IrishGreen8"><u>her e</u></spa n></a> to close this window when finished.
</td>
</tr>
</table>
</body>
</form>
</html>
Here is what a file looks like after I download it:
[MSILoader]
MSIFileName=ProjectInfoSet up.msi
<form name="frmDownload" method="post" action="Download.asp?FID=1 77&FileNam e=Setup.In i" style="display:inline;">
<table width="100%" align="center">
<tr valign="center">
<td align="center">
<br><br><br>
Your file is ready to download. Click here to start
<br><br>
<input type="image" name="Download" src="../Images/DownloadBtn .jpg" value="DOwnload" alt="Download File" align="absmiddle" onclick="ReloadParent();">
<br><br><br><br><br>
Click <a href=javascript:self.close ()><span class="IrishGreen8"><u>her e</u></spa n></a> to close this window when finished.
</td>
</tr>
</table>
</body>
</form>
</html>
I should only have in the file the following text:
[MSILoader]
MSIFileName=ProjectInfoSet up.msi
Can anybody tell me why the HTML from download.asp is getting appended to the end of my downloaded file?
<%@Language="VBScript"%>
<%
Option Explicit
Dim PID
PID = Request.QueryString("PID")
%>
<html>
<head>
<link type="text/css" rel="stylesheet" href="../General/style.css
<TITLE>My Title</TITLE>
<script language="javascript">
function ReloadParent()
{
window.opener.location.hre
}
</script>
</head>
<body leftMargin=0 rightmargin=0 topmargin=0 onLoad="clock();">
<!--#include file="../General/Globals.i
<%
Src = "Download"
%>
<!--#include file="../General/ConnStr.i
<!--#include file="../General/VerifyUsr
<!--#include file="../General/Title.inc
<!--#include file="../General/VerifyUsr
<%
Dim strFileLocation, strRootPath
Dim FID
FID = Request.QueryString("FID")
Dim FileName
FileName = Request.QueryString("FileN
If FID <> "" Then
Dim ConnGetRootPath, SQLGetRootPath, RSGetRootPath
Set ConnGetRootPath = Server.CreateObject("ADODB
ConnGetRootPath.Open ConnStr
SQLGetRootPath = "SELECT Root_Path_For_Files " &_
"FROM t_System_Information"
Set RSGetRootPath = ConnGetRootPath.Execute(SQ
strRootPath = RSGetRootPath("Root_Path_F
RSGetRootPath.Close
Set RSGetRootPath = Nothing
ConnGetRootPath.Close
Set ConnGetRootPath = Nothing
Dim ConnGetFileLocation, SQLGetFileLocation, RSGetFileLocation
Set ConnGetFileLocation = Server.CreateObject("ADODB
ConnGetFileLocation.Open ConnStr
SQLGetFileLocation = "SELECT File_Location " &_
"FROM t_Files " &_
"WHERE [File_ID] = "& FID &""
Set RSGetFileLocation = ConnGetFileLocation.Execut
strFileLocation = RSGetFileLocation("File_Lo
RSGetFileLocation.Close
Set RSGetFileLocation = Nothing
ConnGetFileLocation.Close
If Request("Download.x") = "" Then
' Update record in t_Files that the file is now checked out
Dim ConnCheckOut, SQLCheckOut, RSCheckOut
Set ConnCheckOut = Server.CreateObject("ADODB
ConnCheckOut.Open ConnStr
SQLCheckOut = "exec usp_FileCheckInOut " &_
"@Type = 'CheckOut', " &_
"@FileID = "& FID &", " &_
"@CheckedBy = "& UID &""
Set RSCheckOut = ConnCheckOut.Execute(SQLCh
ConnCheckOut.Close
Set ConnCheckOut = Nothing
End If
Function downloadFile( intFileID, strFilePath, strFile, strDownloadFilename )
Dim strFilename,objStream,objF
Dim intFileLength
' get full path of specified file
strFilename = strFilePath
' clear the buffer
Response.Buffer = True
Response.Clear
' create stream
Set objStream = Server.CreateObject("ADODB
objStream.Open
' set as binary
objStream.Type = 1
' check that the file exists
Set objFilesystem = Server.CreateObject("Scrip
' 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
objstream.Close
Set objstream = Nothing
End Function
If request("Download.x") <> "" Then
Call downloadFile(Request.Query
End If
End If
%>
<form name="frmDownload" method="post" action="Download.asp?FID=<
<table width="100%" align="center">
<tr valign="center">
<td align="center">
<br><br><br>
Your file is ready to download. Click here to start
<br><br>
<input type="image" name="Download" src="../Images/DownloadBtn
<br><br><br><br><br>
Click <a href=javascript:self.close
</td>
</tr>
</table>
</body>
</form>
</html>
Here is what a file looks like after I download it:
[MSILoader]
MSIFileName=ProjectInfoSet
<form name="frmDownload" method="post" action="Download.asp?FID=1
<table width="100%" align="center">
<tr valign="center">
<td align="center">
<br><br><br>
Your file is ready to download. Click here to start
<br><br>
<input type="image" name="Download" src="../Images/DownloadBtn
<br><br><br><br><br>
Click <a href=javascript:self.close
</td>
</tr>
</table>
</body>
</form>
</html>
I should only have in the file the following text:
[MSILoader]
MSIFileName=ProjectInfoSet
Can anybody tell me why the HTML from download.asp is getting appended to the end of my downloaded file?
ASKER
So I want to do a response redirect after calling the function?
your function might becreating some error and hence not closing the stream object thats why its writting the things below it ... try this
__________________________ ______
Function downloadFile( intFileID, strFilePath, strFile, strDownloadFilename )
on error goto errHandler
Dim strFilename,objStream,objF ilesystem, objFilestr eam
Dim intFileLength
' get full path of specified file
strFilename = strFilePath
' 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 that the file exists
Set objFilesystem = Server.CreateObject("Scrip ting.FileS ystemObjec t")
' 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
objstream.Close
Set objstream = Nothing
errHandler:
response.write (err.description)
End Function
__________________________
Function downloadFile( intFileID, strFilePath, strFile, strDownloadFilename )
on error goto errHandler
Dim strFilename,objStream,objF
Dim intFileLength
' get full path of specified file
strFilename = strFilePath
' clear the buffer
Response.Buffer = True
Response.Clear
' create stream
Set objStream = Server.CreateObject("ADODB
objStream.Open
' set as binary
objStream.Type = 1
' check that the file exists
Set objFilesystem = Server.CreateObject("Scrip
' 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
objstream.Close
Set objstream = Nothing
errHandler:
response.write (err.description)
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
actually, I got ASP Error 0156 Header Error when I tried to use the Reponse.Redirect
I actually solved the problem by adding Response.end instead of Response.Redirect. Nevertheless, I am going to assign you the points as you got me thinking in that direction.
I actually solved the problem by adding Response.end instead of Response.Redirect. Nevertheless, I am going to assign you the points as you got me thinking in that direction.
You got that error because of your Response.Buffer setting.
I would've said response.end, but figured you wanted to send the user somewhere friendly instead of blank page..
I would've said response.end, but figured you wanted to send the user somewhere friendly instead of blank page..
If request("Download.x") <> "" Then
Call downloadFile(Request.Query
response.redirect "someotherpage.asp"
End If