views:

220

answers:

1

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).

+1  A: 

You just need this:

File.WriteAllBytes(
     Path.Combine(Server.MapPath("/temp_folder"), "YourFileName.ext"),
     yourByteArray);
Rubens Farias
Whilst I've upvoted this, might I suggest that Server.MapPath("/") is, erm, perhaps not not the right place to be putting a file of this nature? So as above - but replacing the Server.MapPath bit with a more appropriate "specified directory location"
Murph
@ Rubens Farias: Thanks very much for your suggestion - I will try this out. I need to place in a temporary folder so I can add various content (images and text) that is generated by the application. The file will then be either saved back to the database, or made available for download through some other process) . I am assuming you can't work directly with an office document (pre-2007 format), but that you need to save it, open an instance of the office app, and then do various VBA-style operations on the file (along these lines: support.microsoft.com/kb/222929/)
Proposition Joe
@Murph, well, we need to stay inside website vdir; of course, Proposition Joe will save it in a better location. ty for your comments!
Rubens Farias
@Joe, even with pre-2007 formats you can save Office documents as XML, but its a bit more tricky to handle; you probably aware MS doesnt recommends install Office at webserver
Rubens Farias