Hi all,
I'm trying to create an application that takes an entire directory and uploads all of the jpeg file within that directorty to sql server 2k. I have a similar application but it only accepts one image for upload. Here is that code:
Sub UploadImage(ByVal sender As Object, ByVal e As EventArgs)
Dim imgStream As Stream = ImageFile.PostedFile.InputStream
Dim imgData(ImageFile.PostedFile.ContentLength) As Byte
imgStream.Read(imgData, 0, ImageFile.PostedFile.ContentLength)
Dim objConn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("PRE_DSN_Write")
Dim cmdText = "UPDATE tblAgents SET nvcrName = @imgtitle, nvcrType = @imgtype, imgAgentImage = @image WHERE uidAgentID=" & AgentID.Text
Dim objCmd As New SqlClient.SqlCommand(cmdText, objConn)
Dim titleParam As New SqlClient.SqlParameter("@imgtitle", SqlDbType.NVarChar, 100)
Dim typeParam As New SqlClient.SqlParameter("@imgtype", SqlDbType.NVarChar, 100)
Dim imgParam As New SqlClient.SqlParameter("@image", SqlDbType.Image)
titleParam.Value = ImageTitle.Text
typeParam.Value = ImageFile.PostedFile.ContentType
imgParam.Value = imgData
objCmd.Parameters.Add(titleParam)
objCmd.Parameters.Add(typeParam)
objCmd.Parameters.Add(imgParam)
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
End Sub
The next piece of code uses the fso object to loop through all of the files in a directory to upload to SQL. I cannot get this to work. Can anyone help?
Private Sub Images()
Dim objConn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("PRE_DSN_Write")
Dim cmdText = "INSERT INTO ELP_Images (ListingNumber, FileName, ListingImage) VALUES(@LN, @FN , @LI)"
Dim objCmd As New SqlClient.SqlCommand(cmdText, objConn)
objConn.Open()
Dim rIO As New IO.BinaryReader(IO.File.OpenRead(strStoragePath))
Dim fIO As IO.File
Dim fsIO As IO.FileStream
Dim i As Image
Dim objFso As FileSystemObject = Server.CreateObject("Scripting.FileSystemObject"
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
objFolder = objFso.GetFolder(strStoragePath)
Dim ListingNumber As New SqlClient.SqlParameter("@imgtitle", SqlDbType.VarChar, 50)
Dim FileName As New SqlClient.SqlParameter("@imgtype", SqlDbType.VarChar, 50)
Dim ListingImage As New SqlClient.SqlParameter("@image", SqlDbType.Image)
objCmd.Parameters.Add(ListingNumber)
objCmd.Parameters.Add(FileName)
objCmd.Parameters.Add(ListingImage)
For Each objFile In objFolder.Files
'Response.Write(objFile.Type)
If objFile.Type = "JPEG Image" Then
i = objFile
ListingNumber.Value = Left(objFile.Name, InStr(objFile.Name, "." - 1)
FileName.Value = objFile.Name
ListingImage.Value = objFile
objCmd.ExecuteNonQuery()
End If
Next objFile
objConn.Close()
End Sub
I've been through alot of different scenarios so there is probably a lot of uneccessary junk in there. I would appreciate any posts on this. Thank you.
I'm trying to create an application that takes an entire directory and uploads all of the jpeg file within that directorty to sql server 2k. I have a similar application but it only accepts one image for upload. Here is that code:
Sub UploadImage(ByVal sender As Object, ByVal e As EventArgs)
Dim imgStream As Stream = ImageFile.PostedFile.InputStream
Dim imgData(ImageFile.PostedFile.ContentLength) As Byte
imgStream.Read(imgData, 0, ImageFile.PostedFile.ContentLength)
Dim objConn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("PRE_DSN_Write")
Dim cmdText = "UPDATE tblAgents SET nvcrName = @imgtitle, nvcrType = @imgtype, imgAgentImage = @image WHERE uidAgentID=" & AgentID.Text
Dim objCmd As New SqlClient.SqlCommand(cmdText, objConn)
Dim titleParam As New SqlClient.SqlParameter("@imgtitle", SqlDbType.NVarChar, 100)
Dim typeParam As New SqlClient.SqlParameter("@imgtype", SqlDbType.NVarChar, 100)
Dim imgParam As New SqlClient.SqlParameter("@image", SqlDbType.Image)
titleParam.Value = ImageTitle.Text
typeParam.Value = ImageFile.PostedFile.ContentType
imgParam.Value = imgData
objCmd.Parameters.Add(titleParam)
objCmd.Parameters.Add(typeParam)
objCmd.Parameters.Add(imgParam)
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
End Sub
The next piece of code uses the fso object to loop through all of the files in a directory to upload to SQL. I cannot get this to work. Can anyone help?
Private Sub Images()
Dim objConn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("PRE_DSN_Write")
Dim cmdText = "INSERT INTO ELP_Images (ListingNumber, FileName, ListingImage) VALUES(@LN, @FN , @LI)"
Dim objCmd As New SqlClient.SqlCommand(cmdText, objConn)
objConn.Open()
Dim rIO As New IO.BinaryReader(IO.File.OpenRead(strStoragePath))
Dim fIO As IO.File
Dim fsIO As IO.FileStream
Dim i As Image
Dim objFso As FileSystemObject = Server.CreateObject("Scripting.FileSystemObject"
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
objFolder = objFso.GetFolder(strStoragePath)
Dim ListingNumber As New SqlClient.SqlParameter("@imgtitle", SqlDbType.VarChar, 50)
Dim FileName As New SqlClient.SqlParameter("@imgtype", SqlDbType.VarChar, 50)
Dim ListingImage As New SqlClient.SqlParameter("@image", SqlDbType.Image)
objCmd.Parameters.Add(ListingNumber)
objCmd.Parameters.Add(FileName)
objCmd.Parameters.Add(ListingImage)
For Each objFile In objFolder.Files
'Response.Write(objFile.Type)
If objFile.Type = "JPEG Image" Then
i = objFile
ListingNumber.Value = Left(objFile.Name, InStr(objFile.Name, "." - 1)
FileName.Value = objFile.Name
ListingImage.Value = objFile
objCmd.ExecuteNonQuery()
End If
Next objFile
objConn.Close()
End Sub
I've been through alot of different scenarios so there is probably a lot of uneccessary junk in there. I would appreciate any posts on this. Thank you.