Gibberish Pdf file displayed if I transfer using bit stream

C

c676228

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.
 
C

c676228

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?
 
A

Anthony Jones

c676228 said:
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.

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.

 
C

c676228

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>
 
A

Anthony Jones

c676228 said:
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
<%
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:
 
C

c676228

Hooray! Anthony,

That works after I removed the html tag. Mm, I had another good lession.
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top