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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:59:55+00:00 2026-06-12T04:59:55+00:00

The original office file is working with no problems (I tried word and excel),

  • 0

The original office file is working with no problems (I tried word and excel), but when I upload the file to a database as binary and then download it from there to my PC and open the downloaded file, I get warning messages “excel found content cannot read” etc. and the file is getting plainer compared to the original file.

The exact error message is; “Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.”

How I upload the file:

'UPLOAD FILE
Dim fi As New FileInfo(FilePath)
Dim s As Stream = fi.OpenRead()
Dim buffer(fi.Length) As Byte 'I put the buffer in database in varbinary(max) format
s.Read(buffer, 0, fi.Length)
s.Close()

How I download and open the file:

'DOWNLOAD FILE
Dim fi As New FileInfo(FilePath)
Dim s As Stream = fi.OpenWrite()
Dim buffer As Byte() = reader("Binary")
s.Write(buffer, 0, buffer.Length)
s.Close()

'OPEN FILE
Dim p As New Process
p.StartInfo = New ProcessStartInfo(FilePath)
p.Start()

UPDATE:

As suggested, I tried simply copying the file, taking SQL completely out of the mix. Here is the code I tried, which also failed:

Private Sub CopyFile(ByVal filePath As String)
    Dim fi As New FileInfo(filePath)
    Dim s As Stream = fi.OpenRead()
    Dim buffer(CType(fi.Length, Integer) - 1) As Byte
    s.Read(buffer, 0, CType(fi.Length, Integer))
    s.Close()

    Dim fi2 As New FileInfo(filePath & " Copy")
    Dim s2 As Stream = fi2.OpenWrite()
    s2.Write(buffer, 0, buffer.Length)
    s2.Close()
End Sub
  • 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-06-12T04:59:56+00:00Added an answer on June 12, 2026 at 4:59 am

    As described by this MSDN article, VB.NET arrays are declared differently than in other languages, such as in C#. In other languages, when declaring a fixed-length array, the size given is used as the total length of the array. For instance, in c#, the statement fixed byte buffer[3]; would declare a byte array containing 3 elements (with indices 0 through 2). However, in VB, the size given when declaring a fixed array is used as the upper-bound for the array, not the size. Therefore, in VB, the statement Dim buffer(3) As Byte declares a byte array containing 4 elements (with indices 0 through 3).

    With that in mind, if you look closely at your code, you are actually declaring a byte array which is one element larger than the size of the file (with indices 0 through fi.Length). You are then reading the entire file into the byte array, starting at index 0. Therefore, the last byte in the array is left as value 0 (a null character). You then write out the entire contents of the byte array to a new file, including that last null byte. Therefore, your code is correctly copying all the bytes in the file, but is adding an extra null byte to the end of it which makes Excel unhappy. If you look at the original file size and the file size of your newly created file, you will see that your new file is one byte larger than the original.

    To fix this, you simply need to adjust the size of your array when you declare it so that it is exactly the same length as the file:

    Dim buffer(fi.Length - 1) As Byte
    

    However, while I’m at it, I feel obliged to point out some other areas of improvement in the code that you posted. First of all, when using objects which implement the IDisposable interface, such as Stream, it is best to use the Using statement whenever possible. Doing so ensures that the object is always properly closed/disposed, even in the event that an exception is encountered. For instance, it would be better if you did this:

    Dim fi As New FileInfo(FilePath)
    Using s As Stream = fi.OpenRead()
        Dim buffer(fi.Length - 1) As Byte
        s.Read(buffer, 0, fi.Length)
    End Using
    

    Also, it’s obvious that you are not using Option Strict because if you were, you would not be allowed to use fi.Length (a Long) as an argument for the array size or for the buffer length. If you turn on Option Strict, you will be forced to explicitly state that you want to cast the Long value to an Integer. For instance:

    Dim fi As New FileInfo(FilePath)
    Using s As Stream = fi.OpenRead()
        Dim buffer(CInt(fi.Length) - 1) As Byte
        s.Read(buffer, 0, CInt(fi.Length))
    End Using
    

    Turning on Option Strict is a very good idea in most circumstances. It forces you to be aware of your variable types and when data may be lost. For instance, in this case, by having Option Strict turned on, you become aware of the fact that the file size (Long.MaxValue) may be larger than the maximum length of an array (Integer.MaxValue), so, if you want to handle very large files, you would need to write a loop that reads and writes the file in chunks. Even if you don’t want to handle large files, it would be best to check the size first so you can handle the error gracefully rather than allowing an overflow exception to be thrown:

    If fi.Length >= Integer.Max Then
        'Display or log error that the file is too large, and skip loading the file
    Else
        'Load file
    End If
    

    Lastly, if you don’t care about handling large files, there is a simpler way to read and write all the bytes in the file:

    'UPLOAD FILE
    Dim buffer() As Byte = File.ReadAllBytes(filePath)
    
    'DOWNLOAD FILE
    Dim buffer As Byte() = reader("Binary")
    File.WriteAllBytes(filePath, buffer)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Original question So the project I'm working on is deathly paranoid about file uploads.
Due to a Migration from Office 2000 to Office 2010 my word templates names
ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
Simple problem. I'm working on a single SQL Server database which is shared between
Problem saving file back to SharePoint with Excel COM lib. I open the file
I've upgraded an old access 2003 database to access 2010. The original database was
I'm creating a Word 2010 document using C# and Microsoft.Office.Interop.Word . Using the Range.Paste
I've been working with databases for a long time but I'm new to query
I've inherited a large project which uses Office.Interop.Excel to insert data into a spreadsheet.

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.