Sql – Save MS Office files stored in SQL Server directly to server directory with ASP.Net

asp.netms-wordpowerpointsql server

I am storing MS Office files such as PowerPoint (.ppt, .pptx) and Word (.doc, .docx) in SQL Server, and need to save these files to a temp file directory on the server through an ASP.Net application. I get the files up from the database and into the application as a Byte Array. But what I don’t know is how to then save the Byte Array as a file in the appropriate format (.ppt/.pptx etc) in a specified directory location.

I know that I can use code along the lines of setting the Response.ContentType to something like "application/ms-word", and then setting the “content-disposition” of Response.AddHeader to “attachment”. However, this opens a “save as” dialog. What I need to do is to save the file directly into a directory on the server (so that I can do some further operations on it).

Best Answer

You just need this:

File.WriteAllBytes(
     Path.Combine(Server.MapPath("/temp_folder"), "YourFileName.ext"),
     yourByteArray);