Anyone see the error?


S

Scott Meddows

I'm trying to serve a PDF file from a webpage so it will load in the
browser.. Can anyone tell what is wrong with this code? I've tried
everything

Thanks

Dim httpwreq As System.Net.HttpWebRequest =
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
ationReport.pdf"), System.Net.HttpWebRequest)
Dim httpWResp As System.Net.HttpWebResponse =
CType(httpwreq.GetResponse, System.Net.HttpWebResponse)
Dim receiveStream As IO.Stream = httpWResp.GetResponseStream
Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readstream As System.IO.StreamReader = New
System.IO.StreamReader(receiveStream, encode)
Dim read(256) As Char
Dim count As Integer = readstream.Read(read, 0, 256)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", httpWResp.ContentLength.ToString)
Response.AddHeader("content-disposition", "filename=report.PDF")
While (count > 0)
'Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(read))
Dim str As String = New String(read, 0, count)
Response.Write(str) count = readstream.Read(read, 0, 256)
End While
'Response.Flush()
httpWResp.Close()
readstream.Close()
'Response.End()

This is the only code in my page. Can anyone help me out? Thanks
 
Ad

Advertisements

B

bruce barker

as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
 
J

john farrow

Given you have the PDF in 'receiveStream' you can copy it to the response
using something like this, avoiding conversion to UTF-8 or strings but of
which will corrupt the PDF file. This is C# but will work similarly in VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com
 
S

Scott Meddows

How can I convert that array of bytes into an array of char?

john farrow said:
Given you have the PDF in 'receiveStream' you can copy it to the response
using something like this, avoiding conversion to UTF-8 or strings but of
which will corrupt the PDF file. This is C# but will work similarly in VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com

bruce barker said:
as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
 
M

mikeb

Scott said:
How can I convert that array of bytes into an array of char?

You don't want to do this - you want to write the raw bytes.

Use the Response.OutputStream property to get a stream that you can
write byte arrays to.
Given you have the PDF in 'receiveStream' you can copy it to the response
using something like this, avoiding conversion to UTF-8 or strings but of
which will corrupt the PDF file. This is C# but will work similarly in
VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com

as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)


"Scott Meddows" <[email protected]> wrote in
message
I'm trying to serve a PDF file from a webpage so it will load in the
browser.. Can anyone tell what is wrong with this code? I've tried
everything

Thanks

Dim httpwreq As System.Net.HttpWebRequest =
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
ationReport.pdf"), System.Net.HttpWebRequest)
Dim httpWResp As System.Net.HttpWebResponse =
CType(httpwreq.GetResponse, System.Net.HttpWebResponse)
Dim receiveStream As IO.Stream = httpWResp.GetResponseStream
Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readstream As System.IO.StreamReader = New
System.IO.StreamReader(receiveStream, encode)
Dim read(256) As Char
Dim count As Integer = readstream.Read(read, 0, 256)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length",
httpWResp.ContentLength.ToString)

Response.AddHeader("content-disposition", "filename=report.PDF")
While (count > 0)
'Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(read))
Dim str As String = New String(read, 0, count)
Response.Write(str) count = readstream.Read(read, 0, 256)
End While
'Response.Flush()
httpWResp.Close()
readstream.Close()
'Response.End()

This is the only code in my page. Can anyone help me out? Thanks
 
S

Scott Meddows

Response.write only accepts an array of char :(

I'd use response.binarywrite BUT the buffer doesn't always fill up to
capacity...

mikeb said:
Scott said:
How can I convert that array of bytes into an array of char?

You don't want to do this - you want to write the raw bytes.

Use the Response.OutputStream property to get a stream that you can
write byte arrays to.
Given you have the PDF in 'receiveStream' you can copy it to the response
using something like this, avoiding conversion to UTF-8 or strings but of
which will corrupt the PDF file. This is C# but will work similarly in
VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com


as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)


message


I'm trying to serve a PDF file from a webpage so it will load in the
browser.. Can anyone tell what is wrong with this code? I've tried
everything

Thanks

Dim httpwreq As System.Net.HttpWebRequest =
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
ationReport.pdf"), System.Net.HttpWebRequest)
Dim httpWResp As System.Net.HttpWebResponse =
CType(httpwreq.GetResponse, System.Net.HttpWebResponse)
Dim receiveStream As IO.Stream = httpWResp.GetResponseStream
Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readstream As System.IO.StreamReader = New
System.IO.StreamReader(receiveStream, encode)
Dim read(256) As Char
Dim count As Integer = readstream.Read(read, 0, 256)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length",

httpWResp.ContentLength.ToString)

Response.AddHeader("content-disposition", "filename=report.PDF")
While (count > 0)

'Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(read))

Dim str As String = New String(read, 0, count)
Response.Write(str) count = readstream.Read(read, 0, 256)
End While
'Response.Flush()
httpWResp.Close()
readstream.Close()
'Response.End()

This is the only code in my page. Can anyone help me out? Thanks
 
Ad

Advertisements

M

mikeb

Scott said:
Response.write only accepts an array of char :(

I'd use response.binarywrite BUT the buffer doesn't always fill up to
capacity...

Assuming buffer is a byte array containing the data you want written,
and bytes is the number of valid bytes in the array (ie., the array is
not filled):

Response.OutputStream.Write( buffer, 0, bytes)

or
byte [] data = new byte [bytes];
Array.Copy( buffer, data, bytes);
Response.BinaryWrite( data);

or something similar.
Scott said:
How can I convert that array of bytes into an array of char?

You don't want to do this - you want to write the raw bytes.

Use the Response.OutputStream property to get a stream that you can
write byte arrays to.

Given you have the PDF in 'receiveStream' you can copy it to the
response
using something like this, avoiding conversion to UTF-8 or strings but
of
which will corrupt the PDF file. This is C# but will work similarly in

VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com



as you are copying a binary stream, you should use a binary reader and
a
binary writer.

-- bruce (sqlwork.com)



message




I'm trying to serve a PDF file from a webpage so it will load in the
browser.. Can anyone tell what is wrong with this code? I've tried
everything

Thanks

Dim httpwreq As System.Net.HttpWebRequest =
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
ationReport.pdf"), System.Net.HttpWebRequest)
Dim httpWResp As System.Net.HttpWebResponse =
CType(httpwreq.GetResponse, System.Net.HttpWebResponse)
Dim receiveStream As IO.Stream = httpWResp.GetResponseStream
Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readstream As System.IO.StreamReader = New
System.IO.StreamReader(receiveStream, encode)
Dim read(256) As Char
Dim count As Integer = readstream.Read(read, 0, 256)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length",

httpWResp.ContentLength.ToString)


Response.AddHeader("content-disposition", "filename=report.PDF")
While (count > 0)


'Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(read))


Dim str As String = New String(read, 0, count)
Response.Write(str) count = readstream.Read(read, 0, 256)
End While
'Response.Flush()
httpWResp.Close()
readstream.Close()
'Response.End()

This is the only code in my page. Can anyone help me out? Thanks
 
Ad

Advertisements

S

Scott Meddows

YOU ARE THE MAN!!!

Response.OutputStream.Write( buffer, 0, bytes) was the ticket.

I was just using response.write.

Thank you VERY much!!! That was the missing piece.

THANK!!!!!!!!!!!!!!!!!

You kept me from working this weekend :)

mikeb said:
Scott said:
Response.write only accepts an array of char :(

I'd use response.binarywrite BUT the buffer doesn't always fill up to
capacity...

Assuming buffer is a byte array containing the data you want written,
and bytes is the number of valid bytes in the array (ie., the array is
not filled):

Response.OutputStream.Write( buffer, 0, bytes)

or
byte [] data = new byte [bytes];
Array.Copy( buffer, data, bytes);
Response.BinaryWrite( data);

or something similar.
Scott Meddows wrote:

How can I convert that array of bytes into an array of char?

You don't want to do this - you want to write the raw bytes.

Use the Response.OutputStream property to get a stream that you can
write byte arrays to.




Given you have the PDF in 'receiveStream' you can copy it to the
response

using something like this, avoiding conversion to UTF-8 or strings but
of

which will corrupt the PDF file. This is C# but will work similarly in

VB.


byte[] buffer = new byte[1024];

int bytes = 0;

while( ( bytes = receiveStream.Read( buffer, 0,
buffer.Length ) ) > 0 ) {
Response.Write( buffer, 0, bytes );
}

Response.Flush

Regards

John

Visual Programming Ltd mail PO Box 22-222, Khandallah, Wellington, New
Zealand site Level 2, 2 Ganges Road, Khandallah, Wellington, New Zealand
phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com



as you are copying a binary stream, you should use a binary reader
and

a
binary writer.

-- bruce (sqlwork.com)



message




I'm trying to serve a PDF file from a webpage so it will load in the
browser.. Can anyone tell what is wrong with this code? I've tried
everything

Thanks

Dim httpwreq As System.Net.HttpWebRequest =
CType(System.Net.HttpWebRequest.Create("http://lafdev2/ReportServeProxy/Migr
ationReport.pdf"), System.Net.HttpWebRequest)
Dim httpWResp As System.Net.HttpWebResponse =
CType(httpwreq.GetResponse, System.Net.HttpWebResponse)
Dim receiveStream As IO.Stream = httpWResp.GetResponseStream
Dim encode As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readstream As System.IO.StreamReader = New
System.IO.StreamReader(receiveStream, encode)
Dim read(256) As Char
Dim count As Integer = readstream.Read(read, 0, 256)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length",

httpWResp.ContentLength.ToString)


Response.AddHeader("content-disposition", "filename=report.PDF")
While (count > 0)


'Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(read))


Dim str As String = New String(read, 0, count)
Response.Write(str) count = readstream.Read(read, 0, 256)
End While
'Response.Flush()
httpWResp.Close()
readstream.Close()
'Response.End()

This is the only code in my page. Can anyone help me out? Thanks
 

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

Top