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 1032801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:08:10+00:00 2026-05-16T14:08:10+00:00

I’ve been writing and reading PDF’s to a SQL Server 2008 FileStream for a

  • 0

I’ve been writing and reading PDF’s to a SQL Server 2008 FileStream for a few months now without any major problems (apart from tedious user permissions). Yesterday I had a user inform me that some of their PDF’s were being corrupted after being written into the FileStream. So, I did some debugging and I found the problem, but it appears to be a bug with the SqlFileStream libraries that write the file to the FileStream.

Here is my code that writes to the FileStream:

// Byte array representing the FileStream
byte[] fsBytes = (byte[])obj;

SqlFileStream sqlFS = new SqlFileStream(path, fsBytes, FileAccess.Write);

byte[] b = new byte[4096];
int read;

stream.Seek(0, SeekOrigin.Begin);

while ((read = stream.Read(b, 0, b.Length)) > 0) {
    sqlFS.Write(b, 0, read);
}

sqlFS.Close();

From my debugging, I determined that the last iteration reading from the stream has a read value equal to 1253, which means that the last stream read has data in the byte array from index 0 to 1252, and this is correct. Everything at and after 1253 is from the previous read.

So, my understanding is that sqlFS.Write(b, 0, 1253) will write everything from index 0 to 1252 of the byte array into the SqlFileStream. However, it is actually writing everything in the byte array to the SqlFileStream. I have verified this by pulling the PDF back out of the database, and even though I can’t view it normally since its now corrupted, I can still open it in a text editor and view the garbal at the end of it that doesn’t belong there (all of the data that was at position 1253 and after).

I am doing something wrong here, or does the SqlFileStream Write method have a bug like I think it does?

What is odd is that I’ve uploaded quite a few other PDF’s and text files and images and I’ve never seen this problem. I have no idea why it is occurring with some PDF’s and not others…

EDIT:
Here is the code for my read method. The bug may be here as well (thanks to Remus for pointing this out!).

SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Read);
objSqlFileStream.Seek(0, SeekOrigin.Begin);

b = new byte[4096];
int read;

while ((read = objSqlFileStream.Read(b, 0, b.Length)) > 0) {
   Response.BinaryWrite(b);
}

objSqlFileStream.Close();

EDIT #2 (fixed code):

SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Read);
objSqlFileStream.Seek(0, SeekOrigin.Begin);

b = new byte[4096];
int read;

while ((read = objSqlFileStream.Read(b, 0, b.Length)) > 0) {
    if (read < 4096) {
        byte[] b2 = new byte[read];
        System.Buffer.BlockCopy(b, 0, b2, 0, read);
        Response.BinaryWrite(b2);
    }
    else
        Response.BinaryWrite(b);
}

objSqlFileStream.Close();
  • 1 1 Answer
  • 0 Views
  • 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-16T14:08:10+00:00Added an answer on May 16, 2026 at 2:08 pm
    while ((read = objSqlFileStream.Read(b, 0, b.Length)) > 0) {
       Response.BinaryWrite(b);
    }
    

    This writes the entire original byte[] array b and ignores the size read. IS appaling that HttpResponse has no signature .BinaryWrite(byte[], offset, size)… I’m afraid you’ll have to b.Resize(read); before you write it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.