By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Gibberish Pdf file displayed if I transfer using bit stream

P: n/a
Hi all,
The following code was suggested by one of the users in this newsgroup when
a pdf file was requested by a user from an asp page. I used the similar code
in my page and the very interesting thing is when the pdf is displayed on the
fly, the whole page is a gibberish code in stead of a normal pdf file. But it
displays fine if I just use a link to a file on the page. Can you tell me
what's the possible reason will cause this problem?
Thank you.

if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if
--
Betty
Feb 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Oh, I forgot to tell you that I removed the code
Response.Buffer = false
since I set up this is true some where, it tells me that I cannot reset to
false.
So I removed that, not sure if this will impact on the result, I guess not?
--
Betty
"c676228" wrote:
Hi all,
The following code was suggested by one of the users in this newsgroup when
a pdf file was requested by a user from an asp page. I used the similar code
in my page and the very interesting thing is when the pdf is displayed on the
fly, the whole page is a gibberish code in stead of a normal pdf file. But it
displays fine if I just use a link to a file on the page. Can you tell me
what's the possible reason will cause this problem?
Thank you.

if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if

--
Betty
Feb 9 '07 #2

P: n/a

"c676228" <be****@community.nospamwrote in message
news:81**********************************@microsof t.com...
Hi all,
The following code was suggested by one of the users in this newsgroup
when
a pdf file was requested by a user from an asp page. I used the similar
code
in my page and the very interesting thing is when the pdf is displayed on
the
fly, the whole page is a gibberish code in stead of a normal pdf file. But
it
displays fine if I just use a link to a file on the page. Can you tell me
what's the possible reason will cause this problem?
Thank you.

if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if
Do you have other include files in the ASP code?
Do you have other content that is being sent before this code?
The inability to use Response.Buffer = false would indicate that you do.

In the case above Response.Buffer = false in unnecessary since you write the
complete contents of the stream to the response in one call to BinaryWrite.

On IIS6 the default response buffer is 4MB so if your PDF is potentially
larger you will either need to increase this limit or chunk out the PDF
contents.

This is my stock function for doing this:-

Sub SendFileToResponse(FilePath, FileName)

Const clChunkSize = 1048576 ' 1MB

Dim oStream, i
Response.Buffer = False

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"Filename=" & FileName

Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' Binary
oStream.Open
oStream.LoadFromFile FilePath

For i = 1 To oStream.Size \ clChunkSize
Response.BinaryWrite oStream.Read(clChunkSize)
Next
If (oStream.Size Mod clChunkSize) <0 Then
Response.BinaryWrite oStream.Read(oStream.Size Mod clChunkSize)
End If
oStream.Close

End Sub

You would call this function as:-

SendFileToResponse strFilePath, once

since it chunks out the content there is no need to worry about buffer size.
However before you can use it you will need to look at what is happening
earlier in your page that is already placing unwanted content in the
response.

Anthony.

--
Betty

Feb 9 '07 #3

P: n/a
Hi Anthony,
we us IIS 5.0
Thanks for your suggestion. I do have include file, but it has nothing about
buffer set up, no html code, only database connection include file. And I
went to IIS virtual direcotry and saw default configuration for the
application is "enable buffer", so I unchecked this option and it no longer
complains the statement "Response.Buffer = false ".
My pdf file is not big at all, it's less than 90KB. Now after I changed the
code, it gives me very informational error message like this:
Response object error 'ASP 0156 : 80004005'

Header Error

/DeliverPdf.asp, line 48 'which is Response.ContentType =
"application/octet-stream"

The HTTP headers are already written to the client browser. Any HTTP header
modifications must be made before writing page content.
--'end of the eeror message
Then I thought it is because we have default HTTP headers in virtual
directoy, which is something generated by the system like this:
P3P: xxxxx
x-powered by: asp.net
Then I removed that default header and it still display the same error
message.
I don't get this.

My code is as follow:
----deleiverpdf.asp
<!--#INCLUDE VIRTUAL = "/utility/init_test.asp" -->
'init_test is database connection file
<html>
<%
cmdTemp.CommandText="a sql comand to fetch record from system based filename"
dim FileName, strFilePath
'Response.End
Set pdf_info = Server.CreateObject("ADODB.Recordset")
pdf_info.Open cmdTemp, , adOpenKeyset, adLockOptimistic
If pdf_info.EOF Then
response.redirect "/noPicFound404.gif"
Else
pdf_info.MoveFirst()
'Response.Write "File Name: " & pdf_info("pdfFileName")
OK=False
If Request("FileName")=Trim(pdf_info("pdfFileName")) Then
strFilePath = Server.MapPath(pdf_info("TempPdfFilePath"))& "\" &
Request("FileName")
'Response.Write " Path " & strFilePath
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If CInt(pdf_info("MinElapsed")) < CInt(pdf_info("ExpTime")) then
OK=true
End If
If objFSO.FileExists(strFilePath) and OK Then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader
"Content-Disposition","inline;filename="&Request("FileName" ) %>
<body>
<%Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
'Response.Write "I am inside pdf delivery."
Else
response.write "Sorry, the file doesn't exist."
End If
Set objFSO = Nothing
Response.end
End If 'End If FileName is the same as in the database system

End If 'End If pdf_info is EOF
'End Function
Response.Write FileName & "<br>"
Response.Write strFilePath & "<br>"
conn.Close
set conn=Nothing
cmdTemp.Close
set cmdTemp=Nothing
%>
</body>
</html>

--
Betty
"Anthony Jones" wrote:
>
"c676228" <be****@community.nospamwrote in message
news:81**********************************@microsof t.com...
Hi all,
The following code was suggested by one of the users in this newsgroup
when
a pdf file was requested by a user from an asp page. I used the similar
code
in my page and the very interesting thing is when the pdf is displayed on
the
fly, the whole page is a gibberish code in stead of a normal pdf file. But
it
displays fine if I just use a link to a file on the page. Can you tell me
what's the possible reason will cause this problem?
Thank you.

if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if

Do you have other include files in the ASP code?
Do you have other content that is being sent before this code?
The inability to use Response.Buffer = false would indicate that you do.

In the case above Response.Buffer = false in unnecessary since you write the
complete contents of the stream to the response in one call to BinaryWrite.

On IIS6 the default response buffer is 4MB so if your PDF is potentially
larger you will either need to increase this limit or chunk out the PDF
contents.

This is my stock function for doing this:-

Sub SendFileToResponse(FilePath, FileName)

Const clChunkSize = 1048576 ' 1MB

Dim oStream, i
Response.Buffer = False

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"Filename=" & FileName

Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' Binary
oStream.Open
oStream.LoadFromFile FilePath

For i = 1 To oStream.Size \ clChunkSize
Response.BinaryWrite oStream.Read(clChunkSize)
Next
If (oStream.Size Mod clChunkSize) <0 Then
Response.BinaryWrite oStream.Read(oStream.Size Mod clChunkSize)
End If
oStream.Close

End Sub

You would call this function as:-

SendFileToResponse strFilePath, once

since it chunks out the content there is no need to worry about buffer size.
However before you can use it you will need to look at what is happening
earlier in your page that is already placing unwanted content in the
response.

Anthony.

--
Betty


Feb 9 '07 #4

P: n/a

"c676228" <be****@community.nospamwrote in message
news:FF**********************************@microsof t.com...
Hi Anthony,
we us IIS 5.0
Thanks for your suggestion. I do have include file, but it has nothing
about
buffer set up, no html code, only database connection include file. And I
went to IIS virtual direcotry and saw default configuration for the
application is "enable buffer", so I unchecked this option and it no
longer
complains the statement "Response.Buffer = false ".
I strongly recommend you turn that back on again.
My pdf file is not big at all, it's less than 90KB. Now after I changed
the
code, it gives me very informational error message like this:
Response object error 'ASP 0156 : 80004005'

Header Error

/DeliverPdf.asp, line 48 'which is Response.ContentType =
"application/octet-stream"

The HTTP headers are already written to the client browser. Any HTTP
header
modifications must be made before writing page content.
--'end of the eeror message
Then I thought it is because we have default HTTP headers in virtual
directoy, which is something generated by the system like this:
P3P: xxxxx
x-powered by: asp.net
Then I removed that default header and it still display the same error
message.
I don't get this.

My code is as follow:
----deleiverpdf.asp
<!--#INCLUDE VIRTUAL = "/utility/init_test.asp" -->
'init_test is database connection file
<html>
The above is part of the problem you've started to send html content but you
want to send a pdf. Your page should only contain ASP code in <% %>. There
should be no HTML mark up in it at all.
<%
cmdTemp.CommandText="a sql comand to fetch record from system based
filename"
dim FileName, strFilePath
'Response.End
Set pdf_info = Server.CreateObject("ADODB.Recordset")
pdf_info.Open cmdTemp, , adOpenKeyset, adLockOptimistic
If pdf_info.EOF Then
response.redirect "/noPicFound404.gif"
Else
pdf_info.MoveFirst()
'Response.Write "File Name: " & pdf_info("pdfFileName")
OK=False
If Request("FileName")=Trim(pdf_info("pdfFileName")) Then
strFilePath = Server.MapPath(pdf_info("TempPdfFilePath"))& "\" &
Request("FileName")
'Response.Write " Path " & strFilePath
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If CInt(pdf_info("MinElapsed")) < CInt(pdf_info("ExpTime")) then
OK=true
End If
If objFSO.FileExists(strFilePath) and OK Then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader
"Content-Disposition","inline;filename="&Request("FileName" )
Get rid of this bit of markup also

%>
<body>
<%
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
'Response.Write "I am inside pdf delivery."
Else
response.write "Sorry, the file doesn't exist."
End If
Set objFSO = Nothing
Response.end
End If 'End If FileName is the same as in the database system

End If 'End If pdf_info is EOF
'End Function
Response.Write FileName & "<br>"
Response.Write strFilePath & "<br>"
conn.Close
set conn=Nothing
cmdTemp.Close
set cmdTemp=Nothing
%>
And this bit as well:
</body>
</html>


--
Betty
"Anthony Jones" wrote:

"c676228" <be****@community.nospamwrote in message
news:81**********************************@microsof t.com...
Hi all,
The following code was suggested by one of the users in this newsgroup
when
a pdf file was requested by a user from an asp page. I used the
similar
code
in my page and the very interesting thing is when the pdf is displayed
on
the
fly, the whole page is a gibberish code in stead of a normal pdf file.
But
it
displays fine if I just use a link to a file on the page. Can you tell
me
what's the possible reason will cause this problem?
Thank you.
>
>
if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if
>
Do you have other include files in the ASP code?
Do you have other content that is being sent before this code?
The inability to use Response.Buffer = false would indicate that you do.

In the case above Response.Buffer = false in unnecessary since you write
the
complete contents of the stream to the response in one call to
BinaryWrite.

On IIS6 the default response buffer is 4MB so if your PDF is potentially
larger you will either need to increase this limit or chunk out the PDF
contents.

This is my stock function for doing this:-

Sub SendFileToResponse(FilePath, FileName)

Const clChunkSize = 1048576 ' 1MB

Dim oStream, i
Response.Buffer = False

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"Filename=" & FileName

Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' Binary
oStream.Open
oStream.LoadFromFile FilePath

For i = 1 To oStream.Size \ clChunkSize
Response.BinaryWrite oStream.Read(clChunkSize)
Next
If (oStream.Size Mod clChunkSize) <0 Then
Response.BinaryWrite oStream.Read(oStream.Size Mod clChunkSize)
End If
oStream.Close

End Sub

You would call this function as:-

SendFileToResponse strFilePath, once

since it chunks out the content there is no need to worry about buffer
size.
However before you can use it you will need to look at what is happening
earlier in your page that is already placing unwanted content in the
response.

Anthony.

--
Betty

Feb 9 '07 #5

P: n/a
Hooray! Anthony,

That works after I removed the html tag. Mm, I had another good lession.
Thank you.
--
Betty
"Anthony Jones" wrote:
>
"c676228" <be****@community.nospamwrote in message
news:FF**********************************@microsof t.com...
Hi Anthony,
we us IIS 5.0
Thanks for your suggestion. I do have include file, but it has nothing
about
buffer set up, no html code, only database connection include file. And I
went to IIS virtual direcotry and saw default configuration for the
application is "enable buffer", so I unchecked this option and it no
longer
complains the statement "Response.Buffer = false ".

I strongly recommend you turn that back on again.
My pdf file is not big at all, it's less than 90KB. Now after I changed
the
code, it gives me very informational error message like this:
Response object error 'ASP 0156 : 80004005'

Header Error

/DeliverPdf.asp, line 48 'which is Response.ContentType =
"application/octet-stream"

The HTTP headers are already written to the client browser. Any HTTP
header
modifications must be made before writing page content.
--'end of the eeror message
Then I thought it is because we have default HTTP headers in virtual
directoy, which is something generated by the system like this:
P3P: xxxxx
x-powered by: asp.net
Then I removed that default header and it still display the same error
message.
I don't get this.

My code is as follow:
----deleiverpdf.asp
<!--#INCLUDE VIRTUAL = "/utility/init_test.asp" -->
'init_test is database connection file
<html>

The above is part of the problem you've started to send html content but you
want to send a pdf. Your page should only contain ASP code in <% %>. There
should be no HTML mark up in it at all.
<%
cmdTemp.CommandText="a sql comand to fetch record from system based
filename"
dim FileName, strFilePath
'Response.End
Set pdf_info = Server.CreateObject("ADODB.Recordset")
pdf_info.Open cmdTemp, , adOpenKeyset, adLockOptimistic
If pdf_info.EOF Then
response.redirect "/noPicFound404.gif"
Else
pdf_info.MoveFirst()
'Response.Write "File Name: " & pdf_info("pdfFileName")
OK=False
If Request("FileName")=Trim(pdf_info("pdfFileName")) Then
strFilePath = Server.MapPath(pdf_info("TempPdfFilePath"))& "\" &
Request("FileName")
'Response.Write " Path " & strFilePath
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If CInt(pdf_info("MinElapsed")) < CInt(pdf_info("ExpTime")) then
OK=true
End If
If objFSO.FileExists(strFilePath) and OK Then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader
"Content-Disposition","inline;filename="&Request("FileName" )

Get rid of this bit of markup also

%>
<body>
<%

Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
'Response.Write "I am inside pdf delivery."
Else
response.write "Sorry, the file doesn't exist."
End If
Set objFSO = Nothing
Response.end
End If 'End If FileName is the same as in the database system

End If 'End If pdf_info is EOF
'End Function
Response.Write FileName & "<br>"
Response.Write strFilePath & "<br>"
conn.Close
set conn=Nothing
cmdTemp.Close
set cmdTemp=Nothing
%>

And this bit as well:
</body>
</html>

--
Betty
"Anthony Jones" wrote:
>
"c676228" <be****@community.nospamwrote in message
news:81**********************************@microsof t.com...
Hi all,
The following code was suggested by one of the users in this newsgroup
when
a pdf file was requested by a user from an asp page. I used the
similar
code
in my page and the very interesting thing is when the pdf is displayed
on
the
fly, the whole page is a gibberish code in stead of a normal pdf file.
But
it
displays fine if I just use a link to a file on the page. Can you tell
me
what's the possible reason will cause this problem?
Thank you.


if objFSO.FileExists(strFilePath) AND ok then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile strFilePath
Response.Buffer = false
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition","inline;filename="&once
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
else
response.write "Sorry, nonexisting file"
end if
Set objFSO = Nothing
Response.end
end if

>
Do you have other include files in the ASP code?
Do you have other content that is being sent before this code?
The inability to use Response.Buffer = false would indicate that you do.
>
In the case above Response.Buffer = false in unnecessary since you write
the
complete contents of the stream to the response in one call to
BinaryWrite.
>
On IIS6 the default response buffer is 4MB so if your PDF is potentially
larger you will either need to increase this limit or chunk out the PDF
contents.
>
This is my stock function for doing this:-
>
Sub SendFileToResponse(FilePath, FileName)
>
Const clChunkSize = 1048576 ' 1MB
>
Dim oStream, i
Response.Buffer = False
>
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"Filename=" & FileName
>
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' Binary
oStream.Open
oStream.LoadFromFile FilePath
>
For i = 1 To oStream.Size \ clChunkSize
Response.BinaryWrite oStream.Read(clChunkSize)
Next
If (oStream.Size Mod clChunkSize) <0 Then
Response.BinaryWrite oStream.Read(oStream.Size Mod clChunkSize)
End If
oStream.Close
>
End Sub
>
You would call this function as:-
>
SendFileToResponse strFilePath, once
>
since it chunks out the content there is no need to worry about buffer
size.
However before you can use it you will need to look at what is happening
earlier in your page that is already placing unwanted content in the
response.
>
Anthony.
>
>
--
Betty
>
>
>


Feb 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.