0

I have an excel file stored in VARBINARY currently at ID 1. I need to replace this file with an updated version, that has to remain at ID 1. My solution below does replace the byte[] content, but the updated file is unreadable. Any ideas? - ReadFileFully just returns a byte array from a memory stream.

    public ActionResult ReplaceFORATVersion()
    {

        string file_name = "C:\\Calculation Engine FORAT.xls";
        FileStream fs = new FileStream(file_name, FileMode.Open, FileAccess.Read);

        var excelObj = db.FPTExcel.OfType<FPTFORATExcel>().Where(e => e.Id == 1).FirstOrDefault();

        excelObj.Content = ExcelManager.ReadFileFully(fs);

        db.SaveChanges();

        return RedirectToAction("Index", "AdminData");
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
Marc Howard
  • 395
  • 2
  • 6
  • 25
  • Hard to say without knowing what does the `ReadFileFully` method do exactly. – BartoszKP Mar 13 '14 at 14:40
  • Sorry, forgot to mention. Have added it into the question. – Marc Howard Mar 13 '14 at 14:43
  • Perhaps someone will have an idea, but for me it still lacks information. Could you provide the actual implementation of this method? And perhaps the setter for the `Content` property? And finally, how do you conclude that the file is "unreadable"? – BartoszKP Mar 13 '14 at 14:45

1 Answers1

0

It will most likely be to do with the line excelObj.Content = ExcelManager.ReadFileFully(fs);

I would imagine when you are converting the File into a Binary Array there is something in the buffers which is making it unhappy.

You could replace the above with:

public ActionResult ReplaceFORATVersion()
{

    string file_name = "C:\\Calculation Engine FORAT.xls";

    excelObj.Content = File.ReadAllBytes(fileName);

    db.SaveChanges();

    return RedirectToAction("Index", "AdminData");
}

See this post for a better idea:

Best way to read a large file into byte array in C#?

Community
  • 1
  • 1
sarin
  • 5,227
  • 3
  • 34
  • 63
  • Thanks, i only have .net 4.0 though which doesn't have the File class. Any other alternatives? – Marc Howard Mar 13 '14 at 14:53
  • 1
    @MarcHoward It does: http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes%28v=vs.100%29.aspx – BartoszKP Mar 13 '14 at 14:55
  • It's in System.IO.File.ReadAllBytes(fileName) ? – sarin Mar 13 '14 at 15:01
  • I only have access to: Compression, IsolatedStorage, MemoryMappedFiles, Pipes and Ports namespace. I have no idea why though – Marc Howard Mar 13 '14 at 15:06
  • The File class is declared a Static class so should be accessible if the namespace is.... It's possible you have a clashing namespace. I've reproduced the same issue. Try typing it out in full as above. That worked for me. excelObj.Content = System.IO.File.ReadAllBytes(fileName); – sarin Mar 13 '14 at 15:44
  • Thanks, must have been a conflict somewhere! still get the unrecognizeable format issue though – Marc Howard Mar 13 '14 at 16:18
  • Maybe, i read the file in normally that way and it's fine. It's just now that i want to replace the varbinary of the existing excel it doesn't work. – Marc Howard Mar 14 '14 at 10:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49747/discussion-between-sarin-and-marc-howard) – sarin Mar 14 '14 at 13:28