Search code examples
c#asp.netsql-server.docvarbinarymax

How should I be saving .doc documents to a SQL Server database


I am saving a .doc file to a SQL Server database as varbinary(max) using the C# code below.

I am able to save the file, but when I retrieve the file back and want to display the contents on the web page, my code is downloading the file and I am in great confusion about how to handle it.

The exact functionality I am looking for is the way naukri.com uploads the resume and gives a preview of it. My code is :

byte[] fileContent = new byte[fuResume.PostedFile.ContentLength];
fuResume.PostedFile.InputStream.Read(fileContent, 0, fuResume.PostedFile.ContentLength);
//lblAppliedMessage.Text = ByteArrayToString(fileContent);

//lblAppliedMessage.Text = BitConverter.ToString(fileContent).Replace("-", string.Empty);
byte[] btYourDoc;
btYourDoc = fileContent;

Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "inline;filename=yourfilename.doc");
Response.OutputStream.Write(btYourDoc, 0, fileContent.Length);

Response.BinaryWrite(btYourDoc);

Response.End();

Solution

  • squillman is right. There are tons of third party components that do Word -> HTML conversion.

    One other option, which may be more appropriate for an intranet site, is to install Word on the server.

    An example of this is here:

    http://www.c-sharpcorner.com/UploadFile/munnamax/WordToHtml03252007065157AM/WordToHtml.aspx

    Effectively, the doc is opened, saved out as HTML, then subsequent requests can retrieve the HTML version of the file for preview.

    Office automation server side has many pitfalls, however - see http://support.microsoft.com/kb/257757 for more information.