0

I got a response which contains a PDF file

HttpWebResponse res = (HttpWebResponse)HttpWRequest.GetResponse();
Stream pdf = res.GetResponseStream();

I want to show this PDF on web browser without save it in a file. How could I do that?

1

2 Answers 2

1

I'm using the following code and works fine.

public static void SendDataByteFileToClient(HttpContext context, byte[] data, string fileName, string contentType, bool clearHeaders = true)
        {

            if (clearHeaders)
            {
                context.Response.Clear();
                context.Response.ClearHeaders();
            }

            context.Response.BufferOutput = true;
            context.Response.ContentType = contentType;

            context.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
            context.Response.AddHeader("Content-Length", data.Length.ToString());

            if (BrowserHelper.IsOfType(BrowserTypeEnum.IE) && BrowserHelper.Version < 9)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Private);
                context.Response.Cache.SetMaxAge(TimeSpan.FromMilliseconds(1));
            }
            else
            {
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
                context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
                context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately
            }

            if (data.Length > 0)
            {
                context.Response.BinaryWrite(data);
            }

            context.Response.End();
        }
Sign up to request clarification or add additional context in comments.

Comments

0
HttpWebResponse res = (HttpWebResponse)HttpWRequest.GetResponse();
Stream pdfdata = res.GetResponseStream();
string path = pdfdata.ToString();
    WebClient client = new WebClient();
    Byte[] buffer = client.DownloadData(path);

    if (buffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
        Response.End();
    }

4 Comments

But pdf is not a string
pdf is response we getting there not in terms of original pdf
i hope that is common sense. but for your happiness i edit that.
next time i keep in that i write whole program step by step without any single semicolon missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.