Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 973
Tan Avatar asked Jun 05 '26 14:06

Tan


1 Answers

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.

like image 169
squillman Avatar answered Jun 07 '26 10:06

squillman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!