Monday, March 19, 2012

From DB to FileSystem

Hello All,

I have a file stored in the DB, I am converting the document to another type (this already works) but when a conversion is taking place I need the file to go from the DB to the FileSystem (where the conversion takes place).

I have: response.OutputStream.Write

But instead of outputting to the user I want to save it to a sepcifed location.

Thanks in advance,

AdrianDo you mean that you want to save it on the user's machine, or the Web server? You can't do the former, so hopefully you mean the latter.

It depends a little on how you're conferting it, but you should be able to use the File or FileStream classes to do what you want.

Give us more details about what you need to do, and we can be more specific.

Don|||Right my company has developed a system that stores documents in the SQL db. I have created a plug-in for this tool so that when someone uploads a powerpoint document it converts it into an automatic on-line slideshow. Some people already have uploaded work they want converted so I am working on a converter.

So I need to grab the file from the SQLDB, store it in a spcified location and run the convert script (already works).

Does explain what I need it for, so basically, a user clicks on a button, it passed the FileID, I want it to move the file from the SQL server onto a specified folder on the file system where the PPT converter can do it's thing.

Thanks in advance,

Adrian|||Hi Adrian,

Okay, gotcha. The general way to do it is to read the data from the db, which gives you a stream of bytes. Create a FileStream object and write the data to the stream. When you close the stream, the file will be on your drive.

The .NET framework docs for FileStream have a pretty good example of what you need to do.

Don|||OK i'll try and find them, thanks for your help.

Adrian|||I'm having troubles finding the example you suggested, any ideas?

A huge thank you,

Adrian|||Ok I think i've found something, I have a script (see below) that creates the file, now I want to get the file from the database and stream it into the file. I tried this:

dr.Read()

' Make a new FileStream object, exposing our data file.
' If the file exists, open it, and if it doesn't, then Create it.
Dim fs As FileStream = New FileStream("C:\thisisatestppt.ppt", FileMode.CreateNew)

' create the reader and writer, based on our file stream
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim r As BinaryReader = New BinaryReader(fs)

Try
Dim b As Boolean = Convert.ToBoolean(dr("Content"))
w.Write(b)
Catch
End Try

fs.Close()

dr.Close()

But this fails beacuse I think the dr("Content") needs putting into a stream...

Thanks,

Adrian|||Hi Adrian,

First, what is r for? You're not using it, and I don't see any reason to.

One problem is that you're not closing the BinaryWriter. Add this line right before you close the filestream:

w.Close()

I think you may be closing the filestream before you write anything to it.

Don|||yep, sorted it now. I have created the same sort of thing but it creates a buffer and loops through the file until it has completed writing the buffer.

Thanks for you help!

Adrian

No comments:

Post a Comment