webrequest help

  • Thread starter Thread starter rodchar
  • Start date Start date
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
 
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.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
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();
 
Ben said:
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);
 
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)
 
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();
 
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).

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
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)
 
Back
Top