narmi2
asked on
Save MP3 Files In Database
Hi
I am working on a little program which should "hopefully" let the user upload an mp3 file from their harddrive to a database for storage.
Anyone know how to do this?
Thanks
I am working on a little program which should "hopefully" let the user upload an mp3 file from their harddrive to a database for storage.
Anyone know how to do this?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This seems exactly what You need:
http://www.freevbcode.com/ShowCode.asp?ID=1127
http://www.freevbcode.com/ShowCode.asp?ID=1127
ASKER
Thanks for the replies.
In the database, what datatype do I set the column as? image or binary? I'm thinking binary!?
In the database, what datatype do I set the column as? image or binary? I'm thinking binary!?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Ok I now have the following code which does NOT work properly. I upload a 23k file and it returns a 1k file ???
THE CODE -----------------
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
WriteDocumentToDB2("c:\tes t.doc")
End Sub
Private Sub WriteDocumentToDB2(ByVal sInDoc As String)
Dim objDataSet As DataSet
Dim objSQLCommand As SqlCommand
Dim objSQLConnection As New SqlConnection("connection string")
Dim strInsertCommand As String = "insert into table (name, phone, docs) values (@name, @phone, @docs)"
Dim objFileStream As New FileStream(sInDoc, FileMode.OpenOrCreate, FileAccess.Read)
Dim objDocBytes(objFileStream. Length) As Byte
objFileStream.Read(objDocB ytes, 0, objFileStream.Length)
objFileStream.Close()
objSQLCommand = New SqlCommand(strInsertComman d, objSQLConnection)
objSQLCommand.Parameters.A dd(New SqlParameter("@name", SqlDbType.NVarChar, 50))
objSQLCommand.Parameters(" @name").Va lue = "Some Value Here"
objSQLCommand.Parameters.A dd(New SqlParameter("@phone", SqlDbType.NVarChar, 50))
objSQLCommand.Parameters(" @phone").V alue = "Some Value Here"
objSQLCommand.Parameters.A dd(New SqlParameter("@docs", SqlDbType.Image, 16))
objSQLCommand.Parameters(" @docs").Va lue = objDocBytes
objSQLCommand.Connection.O pen()
objSQLCommand.ExecuteNonQu ery()
objSQLCommand.Connection.C lose()
End Sub
Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
ReadDocumentFromDB2("c:\a\ test.doc")
End Sub
Public Sub ReadDocumentFromDB2(ByVal sOutDoc As String)
Dim objDataSet As DataSet
Dim objSQLConnection As New SqlConnection("connection string")
Dim objSQLDataAdapter As SqlDataAdapter
Dim objSelectCommand As String = "select docs from table1 where id = @id"
objSQLDataAdapter = New SqlDataAdapter(objSelectCo mmand, objSQLConnection)
objSQLDataAdapter.SelectCo mmand.Para meters.Add (New SqlParameter("@id", SqlDbType.Int, 4))
objSQLDataAdapter.SelectCo mmand.Para meters("@i d").Value = "0"
objDataSet = New DataSet
objSQLDataAdapter.Fill(obj DataSet)
Dim objDataRow As DataRow
objDataRow = objDataSet.Tables(0).Rows( 0)
Dim objDocBytes() As Byte
objDocBytes = objDataRow("docs")
Dim K As Long
K = UBound(objDocBytes)
Dim objFileStream As New FileStream(sOutDoc, FileMode.OpenOrCreate, FileAccess.Write)
objFileStream.Write(objDoc Bytes, 0, K)
objFileStream.Close()
objFileStream = Nothing
End Sub
Ok I now have the following code which does NOT work properly. I upload a 23k file and it returns a 1k file ???
THE CODE -----------------
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
WriteDocumentToDB2("c:\tes
End Sub
Private Sub WriteDocumentToDB2(ByVal sInDoc As String)
Dim objDataSet As DataSet
Dim objSQLCommand As SqlCommand
Dim objSQLConnection As New SqlConnection("connection string")
Dim strInsertCommand As String = "insert into table (name, phone, docs) values (@name, @phone, @docs)"
Dim objFileStream As New FileStream(sInDoc, FileMode.OpenOrCreate, FileAccess.Read)
Dim objDocBytes(objFileStream.
objFileStream.Read(objDocB
objFileStream.Close()
objSQLCommand = New SqlCommand(strInsertComman
objSQLCommand.Parameters.A
objSQLCommand.Parameters("
objSQLCommand.Parameters.A
objSQLCommand.Parameters("
objSQLCommand.Parameters.A
objSQLCommand.Parameters("
objSQLCommand.Connection.O
objSQLCommand.ExecuteNonQu
objSQLCommand.Connection.C
End Sub
Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
ReadDocumentFromDB2("c:\a\
End Sub
Public Sub ReadDocumentFromDB2(ByVal sOutDoc As String)
Dim objDataSet As DataSet
Dim objSQLConnection As New SqlConnection("connection string")
Dim objSQLDataAdapter As SqlDataAdapter
Dim objSelectCommand As String = "select docs from table1 where id = @id"
objSQLDataAdapter = New SqlDataAdapter(objSelectCo
objSQLDataAdapter.SelectCo
objSQLDataAdapter.SelectCo
objDataSet = New DataSet
objSQLDataAdapter.Fill(obj
Dim objDataRow As DataRow
objDataRow = objDataSet.Tables(0).Rows(
Dim objDocBytes() As Byte
objDocBytes = objDataRow("docs")
Dim K As Long
K = UBound(objDocBytes)
Dim objFileStream As New FileStream(sOutDoc, FileMode.OpenOrCreate, FileAccess.Write)
objFileStream.Write(objDoc
objFileStream.Close()
objFileStream = Nothing
End Sub
ASKER
To be more precise it uploads 23.040 bytes and returns 15 bytes! That 15 bytes is very close to the 16 byte image datatype: i.e. objSQLCommand.Parameters.A dd(New SqlParameter("@docs", SqlDbType.Image, 16))
Any ideas?
Any ideas?
ASKER
sorry i mean 23,040 bytes and 15 bytes
ASKER
If I could offer more points I would, as this is very urgent. anyone know from the code posted above, why I only get a 15 byte file when i upload a 23,050 byte file ???
Please help.
Please help.
ASKER
IT'S ALIVE !!!
THANKS :D
THANKS :D
You would do this in the same way as images - i.e convert the files data into binary.
You would then store it in the database.
Examples of how to achieve this:
http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1
http://www.eggheadcafe.com/articles/20041022.asp
regards,
KS