webrequest help

R

rodchar

hi all,

given:
HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create("myurl");
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Response.Write(WebResp.StatusCode);
Response.Write(WebResp.Server);

Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);

this url is retrieving a .pdf file. how do i get my pdf file to display on
my web page?

thanks,
rodchar
 
C

Cowboy \(Gregory A. Beamer\)

When you stream directly to the stream, you have to set the MIME type in the
page before the code that starts the stream. Once you have set the type, the
browser should recongnize the file as whatever type you have told the
browser that file is.

If that is overbearing, you can stream the file to a folder in the website
and redirect the user to that file. Just make sure you have a mechanism that
cleans up old files.
 
R

rodchar

once i set the MIME type, how, then, do i take the stream and display it my
web page?
 
B

Ben Amada

rodchar said:
once i set the MIME type, how, then, do i take the stream and display it
my web page?

You read the stream into a byte array, and then output the byte array into
the Response object. For a previous project, I found that adding a
content-disposition header helped (for Safari, I think).

.... your previous code ...
Stream Answer = WebResp.GetResponseStream();

int PdfByteCount = (int)Answer.Length - 1;
byte[] pdfBytes = new byte[PdfByteCount];
Answer.Read(pdfBytes, 0, PdfByteCount);

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader(
"Content-disposition",
"inline; filename=some_file_name.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(pdfBytes);
Response.End();
 
B

Ben Amada

Ben Amada wrote:
int PdfByteCount = (int)Answer.Length - 1;
byte[] pdfBytes = new byte[PdfByteCount];
Answer.Read(pdfBytes, 0, PdfByteCount);

That's not correct, should be:

byte[] pdfBytes = new byte[Answer.Length];
Answer.Read(pdfBytes, 0, (int)Answer.Length);
 
B

bruce barker

a pdf is webrequest response. it will replace the page with the pdf.
normally, you would have a link the user hits that returns the pdf file.
if you want it inline in the page, then you add an iframe to page, and
set the resource the pdf url.

-- bruce (sqlwork.com)
 
R

rodchar

i'm getting message:
This stream does not support seek operations.

Here's some of what i'm doing at the source of the url:

context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = @"application/pdf";
//context.Response.AddHeader("content-disposition",
String.Format("inline; filename={0}.pdf", adNumber));
context.Response.AddHeader("content-disposition",
String.Format("attachment; filename={0}.pdf", adNumber));

byte[] image = new byte[fileInfo.Length];
using (BinaryReader reader = new BinaryReader(new
FileStream(path, FileMode.Open, FileAccess.Read)))
{
image = reader.ReadBytes((int)fileInfo.Length);
}

context.Response.BinaryWrite(image);
context.Response.Flush();
context.Response.Close();
context.Response.End();
 
C

Cowboy \(Gregory A. Beamer\)

You can move data from a stream to another stream. Outputting as bytes is
another option, per Ben's post (have not gone through his code yet to ensure
it is correct).


rodchar said:
i'm getting message:
This stream does not support seek operations.

Here's some of what i'm doing at the source of the url:

context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = @"application/pdf";
//context.Response.AddHeader("content-disposition",
String.Format("inline; filename={0}.pdf", adNumber));
context.Response.AddHeader("content-disposition",
String.Format("attachment; filename={0}.pdf", adNumber));

byte[] image = new byte[fileInfo.Length];
using (BinaryReader reader = new BinaryReader(new
FileStream(path, FileMode.Open, FileAccess.Read)))
{
image = reader.ReadBytes((int)fileInfo.Length);
}

context.Response.BinaryWrite(image);
context.Response.Flush();
context.Response.Close();
context.Response.End();


bruce barker said:
a pdf is webrequest response. it will replace the page with the pdf.
normally, you would have a link the user hits that returns the pdf file.
if you want it inline in the page, then you add an iframe to page, and
set the resource the pdf url.

-- bruce (sqlwork.com)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top