Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:58:02+00:00 2026-05-13T13:58:02+00:00

I am using .NET 3.5 ASP.NET. Currently my web site serves a PDF file

  • 0

I am using .NET 3.5 ASP.NET. Currently my web site serves a PDF file in the following manner:

context.Response.WriteFile(@"c:\blah\blah.pdf");

This works great. However, I’d like to serve it via the context.Response.Write(char [], int, int) method.

So I tried sending out the file via

byte [] byteContent = File.ReadAllBytes(ReportPath);
ASCIIEncoding encoding = new ASCIIEncoding();
char[] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent, 0, charContent.Length);

That did not work (e.g. browser’s PDF plugin complains that the file is corrupted).

So I tried the Unicode approach:

byte [] byteContent = File.ReadAllBytes(ReportPath);
UnicodeEncoding encoding = new UnicodeEncoding();
char[] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent, 0, charContent.Length);

which also did not work.

What am I missing?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:58:03+00:00Added an answer on May 13, 2026 at 1:58 pm

    You should not convert the bytes into characters, that is why it becomes “corrupted”. Even though ASCII characters are stored in bytes the actual ASCII character set is limited to 7 bits. Thus, converting a byte stream with the ASCIIEncoding will effectively remove the 8th bit from each byte.

    The bytes should be written to the OutputStream stream of the Response instance.

    Instead of loading all bytes from the file upfront, which could possibly consume a lot of memory, reading the file in chunks from a stream is a better approach. Here’s a sample of how to read from one stream and then write to another:

    void LoadStreamToStream(Stream inputStream, Stream outputStream)
    {
        const int bufferSize = 64 * 1024;
        var buffer = new byte[bufferSize];
    
        while (true)
        {
            var bytesRead = inputStream.Read(buffer, 0, bufferSize);
            if (bytesRead > 0)
            {
                outputStream.Write(buffer, 0, bytesRead);
            }
            if ((bytesRead == 0) || (bytesRead < bufferSize))
                break;
        }
    }
    

    You can then use this method to load the contents of your file directly to the Response.OutputStream

    LoadStreamToStream(fileStream, Response.OutputStream);
    

    Better still, here’s a method opening a file and loading its contents to a stream:

    void LoadFileToStream(string inputFile, Stream outputStream)
    {
        using (var streamInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
        {
            LoadStreamToStream(streamInput, outputStream);
            streamInput.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing a web site using ASP.NET 3.5. On a page there
I'm currently working on web application using VB in ASP.NET. Right now I have
Im currently using vs2008 with asp.net mvc framework for web development. Im missing a
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
I am currently trying to establish a connection between an ASP.NET web site project
Using ASP.Net Am New to website development Currently am developing a web pages, when
I am currently developing an ASP.NET MVC 3 site using Visual Studio 2010 with
I'm trying to send email to my site users (ASP.NET, VS2010), currently I'm using
I have an ASP.NET 4 web app. I am using a Master Page Site.Master.
I have a ASP.Net 2.0 website that is currently using a custom MembershipProvider and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.