468,272 Members | 2,136 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Anyone see the error?

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.Ge tBytes(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
Nov 18 '05 #1
7 1097
as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
"Scott Meddows" <sc******************@tsged-removeme.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
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.Ge tBytes(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

Nov 18 '05 #2
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" <no***********@safeco.com> wrote in message
news:ej***************@tk2msftngp13.phx.gbl...
as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
"Scott Meddows" <sc******************@tsged-removeme.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
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.Ge tBytes(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


Nov 18 '05 #3
How can I convert that array of bytes into an array of char?

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
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" <no***********@safeco.com> wrote in message
news:ej***************@tk2msftngp13.phx.gbl...
as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
"Scott Meddows" <sc******************@tsged-removeme.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
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.Ge tBytes(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



Nov 18 '05 #4
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.

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
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" <no***********@safeco.com> wrote in message
news:ej***************@tk2msftngp13.phx.gbl...
as you are copying a binary stream, you should use a binary reader and a
binary writer.

-- bruce (sqlwork.com)
"Scott Meddows" <sc******************@tsged-removeme.com> wrote in
message
news:uA**************@TK2MSFTNGP11.phx.gbl...

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.Ge tBytes(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


--
mikeb
Nov 18 '05 #5
Response.write only accepts an array of char :(

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

"mikeb" <ma************@mailnull.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
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.

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Given you have the PDF in 'receiveStream' you can copy it to the responseusing something like this, avoiding conversion to UTF-8 or strings but ofwhich 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" <no***********@safeco.com> wrote in message
news:ej***************@tk2msftngp13.phx.gbl...

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

-- bruce (sqlwork.com)
"Scott Meddows" <sc******************@tsged-removeme.com> wrote in


message
news:uA**************@TK2MSFTNGP11.phx.gbl...

>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.Ge tBytes(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
>
>


--
mikeb

Nov 18 '05 #6
Scott Meddows wrote:
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.
"mikeb" <ma************@mailnull.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
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.

"john farrow" <vi*******@xtra.co.nz> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
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" <no***********@safeco.com> wrote in message
news:ej***************@tk2msftngp13.phx.gbl. ..
>as you are copying a binary stream, you should use a binary reader and
a
binary writer.
>
>-- bruce (sqlwork.com)
>
>
>"Scott Meddows" <sc******************@tsged-removeme.com> wrote in

message
>news:uA**************@TK2MSFTNGP11.phx.gbl. ..
>
>
>>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
>>
>>
>
>


--
mikeb


--
mikeb
Nov 18 '05 #7
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" <ma************@mailnull.com> wrote in message
news:O2**************@TK2MSFTNGP11.phx.gbl...
Scott Meddows wrote:
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.
"mikeb" <ma************@mailnull.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
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.
"john farrow" <vi*******@xtra.co.nz> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
>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" <no***********@safeco.com> wrote in message
>news:ej***************@tk2msftngp13.phx.gbl. ..
>
>
>>as you are copying a binary stream, you should use a binary reader
and
a
>>binary writer.
>>
>>-- bruce (sqlwork.com)
>>
>>
>>"Scott Meddows" <sc******************@tsged-removeme.com> wrote in

message
>>news:uA**************@TK2MSFTNGP11.phx.gbl. ..
>>
>>
>>>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
>>>
>>>
>>
>>
--
mikeb


--
mikeb

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by tomakated | last post: by
1 post views Thread by MPutt | last post: by
20 posts views Thread by Pete Marsh | last post: by
1 post views Thread by Blubaugh, David A. | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.