Question

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();
Was it helpful?

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.

OTHER TIPS

The reason your file is getting downloaded instead of displayed is because you're setting the content type to application/ms-word. This tells a browser to download the file (they can't natively handle files of that type so they delegate to an external app).

You'll need to have code that knows how to interpret the MS Word format and convert that to something viewable in a browser (HTML, some kind of plugin that will do that for you, etc). Saving the raw Word document and then sending it back to the client in the same state is basically just having them download a Word file.

Here's a good one where the end result it's up to the user whether to download or view the file here's the link but @Squillman is right by putting the Response headers you're telling it to download.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top