Drag/Drop/Upload Functionallity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Webbyz

    #1

    Drag/Drop/Upload Functionallity

    Hello, I was wondering if there is a way that I can enable my
    application to accept a file such as a .pdf that when dropped onto the
    document, it will upload the file to my database or a directory.

    I am trying to make the app as user friendly as possable and would
    just like the users to be able to drop the file into an area of the
    screen and it would just accept the document and maybe prompt if there
    is an existing file for that record in the database.

    Any help would be apperciated.

    -Matt

  • Patrice

    #2
    Re: Drag/Drop/Upload Functionallity

    Assuming this is a windows application and that the problem is with
    drag/drop handling, try :

    http://www.vbforums.com/showthread.php?t=328593

    ---
    Patrice


    "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
    1174405641.4782 33.319580@e65g2 00...legr oups.com...
    Hello, I was wondering if there is a way that I can enable my
    application to accept a file such as a .pdf that when dropped onto the
    document, it will upload the file to my database or a directory.
    >
    I am trying to make the app as user friendly as possable and would
    just like the users to be able to drop the file into an area of the
    screen and it would just accept the document and maybe prompt if there
    is an existing file for that record in the database.
    >
    Any help would be apperciated.
    >
    -Matt
    >

    Comment

    • Webbyz

      #3
      Re: Drag/Drop/Upload Functionallity



      It seems like the example shows just picture handeling. But this does
      not have any way to absorb the file directly into a directory or Db.

      Comment

      • Patrice

        #4
        Re: Drag/Drop/Upload Functionallity

        Sorry, was not sure if the problem was also with those operations or if you
        had already something but wanted to add drag/drop capability...

        To copy a file just see the documentation for System.IO.File (or the My
        namespaces if VB 2005).
        For the BLOB part see the ADO.NET documentation :
        http://msdn2.microsoft.com/en-us/library/4f5s1we0.aspx

        ---
        Patrice

        "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
        1174407613.4061 96.299360@o5g20 00...legro ups.com...
        >
        >
        It seems like the example shows just picture handeling. But this does
        not have any way to absorb the file directly into a directory or Db.
        >

        Comment

        • Webbyz

          #5
          Re: Drag/Drop/Upload Functionallity

          It seems like this function is hanging up where it wants to import the
          data with the type Byte.

          I keep getting the once I select the file to import and it attempts to
          read into the Db. It just doesnt like the type Byte for some reason.

          Any other ideas?

          Comment

          • Patrice

            #6
            Re: Drag/Drop/Upload Functionallity

            Doesn't like that is ? Do you have any error message ? What is the DB you
            are using ? What is the size of the file you tried ?

            ---
            Patrice


            "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
            1174420654.7235 17.199310@o5g20 00...legro ups.com...
            It seems like this function is hanging up where it wants to import the
            data with the type Byte.
            >
            I keep getting the once I select the file to import and it attempts to
            read into the Db. It just doesnt like the type Byte for some reason.
            >
            Any other ideas?
            >

            Comment

            • Webbyz

              #7
              Re: Drag/Drop/Upload Functionallity

              The size of the file is a .pdf which is about 750kb.

              Error as follows.
              "An error was thrown by the ADO.Net component in the GetData function.
              Error Details: Syntax error (missing operator) in query expression
              "System.Byt e[]""

              Database is a Access 2000 .mdb With three fields.
              1.) ID - AutoNumber
              2.) Description - Text
              3.) File - OLEObject

              Here is my code so far.

              Imports System.IO
              Public Class Form1

              Public Shared Sub AddFile(ByVal pdfFilePath As String)

              Dim newFile() As Byte = GetFile(pdfFile Path)

              UpdateData("INS ERT INTO Table1 (Description, File) VALUES('" &
              My.Computer.Fil eSystem.GetFile Info(pdfFilePat h).Name & "'," &
              newFile.ToStrin g & ")")

              End Sub

              Public Shared Function GetFile(ByVal filePath As String) As Byte()
              Dim stream As FileStream = New FileStream( _
              filePath, FileMode.Open, FileAccess.Read )
              Dim reader As BinaryReader = New BinaryReader(st ream)

              Dim newFile() As Byte = reader.ReadByte s(stream.Length )

              reader.Close()
              stream.Close()

              Return newFile
              End Function


              Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
              As System.EventArg s) Handles Button1.Click
              Dim OFD As New OpenFileDialog
              With OFD
              .Title = "Select file..."
              .Filter = "All Files (*.*)|*.*|PDF Documents (*.pdf)|
              *.pdf"
              .ShowHelp = False
              .ShowReadOnly = False
              .ShowDialog()
              .Multiselect = False
              If File.Exists(.Fi leName) Then
              AddFile(.FileNa me)
              Else
              MsgBox("File not found!")
              End If
              End With
              End Sub

              Private Sub Button2_Click(B yVal sender As System.Object, ByVal e
              As System.EventArg s) Handles Button2.Click
              Dim myDataset As DataSet
              ' Grab all records in this table with ID 1 or whatever number
              you want
              myDataset = GetData("SELECT * FROM [table1] WHERE [ID]=1",
              "myFile")
              If myDataset.Table s("myFile").Row s.Count 0 Then ' found the
              File
              Label1.Text =
              myDataset.Table s("myFile").Row s(0).Item("Desc ription").ToStr ing
              ' Get a temp file form windows
              Dim TempFileName As String =
              My.Computer.Fil eSystem.GetTemp FileName
              ' get the file data and put it into a file or something
              Label2.Text =
              myDataset.Table s("myFile").Row s(0).Item("File ").ToString
              Else
              MsgBox("No record found!")
              End If
              End Sub

              Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
              System.EventArg s) Handles MyBase.Load

              End Sub
              End Class

              Comment

              • Patrice

                #8
                Re: Drag/Drop/Upload Functionallity

                And what does GetData ? Are you using option strict ? It would seem you use
                a byte at an inappropriate place.

                The simplest sample I could come with is :
                Dim Connection As New OleDbConnection (ConnectionStri ng)
                Dim Command As New OleDbCommand("S ELECT TOP 1 Blob FROM Table1",
                Connection)
                Connection.Open ()
                Dim Reader As OleDb.OleDbData Reader =
                Command.Execute Reader(CommandB ehavior.Sequent ialAccess)
                Reader.Read()
                Dim Blob(255) As Byte
                Reader.GetBytes (0, 0, Blob, 0, 255)
                Reader.Close()
                Command.Dispose ()
                Connection.Clos e()
                For i As Integer = 0 To UBound(Blob)
                If Blob(i) <i Then MsgBox("Doesn't match")
                Debug.Print(i & "/" & Blob(i))
                Next

                The final check just allows to see that data are retrieved correctly (I
                stored a small buffer with value n at the nth position).

                Finally it's likely better to retrieve regular data using a query and to
                handle blob data with another query so that you don't incurs the overhead of
                processing potentially huge values when this is not really needed...

                ---
                Patrice

                "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
                1174480390.8223 62.237890@o5g20 00...legro ups.com...
                The size of the file is a .pdf which is about 750kb.
                >
                Error as follows.
                "An error was thrown by the ADO.Net component in the GetData function.
                Error Details: Syntax error (missing operator) in query expression
                "System.Byt e[]""
                >
                Database is a Access 2000 .mdb With three fields.
                1.) ID - AutoNumber
                2.) Description - Text
                3.) File - OLEObject
                >
                Here is my code so far.
                >
                Imports System.IO
                Public Class Form1
                >
                Public Shared Sub AddFile(ByVal pdfFilePath As String)
                >
                Dim newFile() As Byte = GetFile(pdfFile Path)
                >
                UpdateData("INS ERT INTO Table1 (Description, File) VALUES('" &
                My.Computer.Fil eSystem.GetFile Info(pdfFilePat h).Name & "'," &
                newFile.ToStrin g & ")")
                >
                End Sub
                >
                Public Shared Function GetFile(ByVal filePath As String) As Byte()
                Dim stream As FileStream = New FileStream( _
                filePath, FileMode.Open, FileAccess.Read )
                Dim reader As BinaryReader = New BinaryReader(st ream)
                >
                Dim newFile() As Byte = reader.ReadByte s(stream.Length )
                >
                reader.Close()
                stream.Close()
                >
                Return newFile
                End Function
                >
                >
                Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
                As System.EventArg s) Handles Button1.Click
                Dim OFD As New OpenFileDialog
                With OFD
                .Title = "Select file..."
                .Filter = "All Files (*.*)|*.*|PDF Documents (*.pdf)|
                *.pdf"
                .ShowHelp = False
                .ShowReadOnly = False
                .ShowDialog()
                .Multiselect = False
                If File.Exists(.Fi leName) Then
                AddFile(.FileNa me)
                Else
                MsgBox("File not found!")
                End If
                End With
                End Sub
                >
                Private Sub Button2_Click(B yVal sender As System.Object, ByVal e
                As System.EventArg s) Handles Button2.Click
                Dim myDataset As DataSet
                ' Grab all records in this table with ID 1 or whatever number
                you want
                myDataset = GetData("SELECT * FROM [table1] WHERE [ID]=1",
                "myFile")
                If myDataset.Table s("myFile").Row s.Count 0 Then ' found the
                File
                Label1.Text =
                myDataset.Table s("myFile").Row s(0).Item("Desc ription").ToStr ing
                ' Get a temp file form windows
                Dim TempFileName As String =
                My.Computer.Fil eSystem.GetTemp FileName
                ' get the file data and put it into a file or something
                Label2.Text =
                myDataset.Table s("myFile").Row s(0).Item("File ").ToString
                Else
                MsgBox("No record found!")
                End If
                End Sub
                >
                Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
                System.EventArg s) Handles MyBase.Load
                >
                End Sub
                End Class
                >

                Comment

                • Webbyz

                  #9
                  Re: Drag/Drop/Upload Functionallity

                  Well at this point the GetData is gathering the Data and passing it
                  into the Db.

                  Here is the Module for that function.


                  Module DataAccessModul e
                  Friend Function GetData(ByVal strSQL As String, ByVal TableName As
                  String) As DataSet
                  Dim myDataset As DataSet = New DataSet
                  ' Create our connection string
                  Dim myConnString As OleDbConnection = New
                  OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=c:
                  \db1.mdb")
                  ' Create our command
                  Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                  myConnString)
                  ' Create our data adapter
                  Dim myDataAdapter As OleDbDataAdapte r = New OleDbDataAdapte r
                  ' Assign the SQL command to the data adapter
                  myDataAdapter.S electCommand = myCommand
                  Try
                  ' Fill the dataset with the data from our command
                  myDataAdapter.F ill(myDataset, TableName)
                  ' Return the dataset
                  GetData = myDataset
                  Catch ex As OleDbException
                  MsgBox("An error was thrown by the ADO.Net component in
                  the GetData function. Error Details: " & _
                  vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                  Debug.WriteLine ("SQL Statement: " & strSQL)
                  GetData = Nothing
                  Finally
                  ' Close the connection and destroy our connection objects
                  myCommand.Conne ction.Close()
                  myDataset = Nothing
                  myConnString = Nothing
                  myCommand = Nothing
                  myDataAdapter = Nothing
                  End Try
                  End Function

                  Friend Function UpdateData(ByVa l strSQL As String) As Integer
                  ' Create our connection string
                  Dim myConnString As OleDbConnection = New
                  OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=c:
                  \db1.mdb")
                  ' Create our command
                  Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                  myConnString)
                  Try
                  ' Open the connection
                  myCommand.Conne ction.Open()
                  ' execute the SQL
                  UpdateData = myCommand.Execu teNonQuery()
                  Catch ex As OleDbException
                  MsgBox("An error was thrown by the ADO.Net component in
                  the GetData function. Error Details: " & _
                  vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                  Debug.WriteLine ("SQL Statement: " & strSQL)
                  UpdateData = Nothing
                  Finally
                  ' Close the connection and destroy our connection object
                  myCommand.Conne ction.Close()
                  myConnString = Nothing
                  myCommand = Nothing
                  End Try
                  End Function

                  End Module

                  Comment

                  • Patrice

                    #10
                    Re: Drag/Drop/Upload Functionallity

                    Ok the error message is actually misleading as the method name is not
                    correct. This is actually in the insertion statement where you use to string
                    that dumps the type name inside the SQL statement resulting in a SQL syntax
                    error...

                    Please post a message if you solve the problem. I'll try to post a sample
                    later today so that you can go back on better tracks...
                    ----
                    Patrice

                    "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
                    1174499501.1206 35.254940@d57g2 00...legr oups.com...
                    Well at this point the GetData is gathering the Data and passing it
                    into the Db.
                    >
                    Here is the Module for that function.
                    >
                    >
                    Module DataAccessModul e
                    Friend Function GetData(ByVal strSQL As String, ByVal TableName As
                    String) As DataSet
                    Dim myDataset As DataSet = New DataSet
                    ' Create our connection string
                    Dim myConnString As OleDbConnection = New
                    OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=c:
                    \db1.mdb")
                    ' Create our command
                    Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                    myConnString)
                    ' Create our data adapter
                    Dim myDataAdapter As OleDbDataAdapte r = New OleDbDataAdapte r
                    ' Assign the SQL command to the data adapter
                    myDataAdapter.S electCommand = myCommand
                    Try
                    ' Fill the dataset with the data from our command
                    myDataAdapter.F ill(myDataset, TableName)
                    ' Return the dataset
                    GetData = myDataset
                    Catch ex As OleDbException
                    MsgBox("An error was thrown by the ADO.Net component in
                    the GetData function. Error Details: " & _
                    vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                    Debug.WriteLine ("SQL Statement: " & strSQL)
                    GetData = Nothing
                    Finally
                    ' Close the connection and destroy our connection objects
                    myCommand.Conne ction.Close()
                    myDataset = Nothing
                    myConnString = Nothing
                    myCommand = Nothing
                    myDataAdapter = Nothing
                    End Try
                    End Function
                    >
                    Friend Function UpdateData(ByVa l strSQL As String) As Integer
                    ' Create our connection string
                    Dim myConnString As OleDbConnection = New
                    OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=c:
                    \db1.mdb")
                    ' Create our command
                    Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                    myConnString)
                    Try
                    ' Open the connection
                    myCommand.Conne ction.Open()
                    ' execute the SQL
                    UpdateData = myCommand.Execu teNonQuery()
                    Catch ex As OleDbException
                    MsgBox("An error was thrown by the ADO.Net component in
                    the GetData function. Error Details: " & _
                    vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                    Debug.WriteLine ("SQL Statement: " & strSQL)
                    UpdateData = Nothing
                    Finally
                    ' Close the connection and destroy our connection object
                    myCommand.Conne ction.Close()
                    myConnString = Nothing
                    myCommand = Nothing
                    End Try
                    End Function
                    >
                    End Module
                    >

                    Comment

                    • Patrice

                      #11
                      Re: Drag/Drop/Upload Functionallity

                      Ok so I made oledb samples from the SQLClient samples I discussed earlier in
                      the thread. It would give us for writing :

                      Dim BlobIn(255) As Byte
                      For i As Integer = 0 To UBound(BlobIn)
                      BlobIn(i) = CByte(i Mod 255)
                      Next
                      Dim Connection As New OleDbConnection (ConnectionStri ng)
                      Dim Command As New OleDbCommand("I NSERT INTO Table1(Blob) VALUES
                      (?)", Connection)
                      Connection.Open ()
                      Command.Paramet ers.Add("a", OleDbType.Binar y, UBound(BlobIn)) .Value
                      = BlobIn
                      Command.Execute NonQuery()


                      For reading :
                      Dim BlobOut(1024) As Byte
                      Command = New OleDbCommand("S ELECT TOP 1 Blob FROM Table1",
                      Connection) ' Would select using the PK
                      Dim Reader As OleDbDataReader = Command.Execute Reader
                      Reader.Read()

                      Debug.Print(Rea der.GetBytes(0, 0, BlobOut, 0,
                      BlobOut.Length) .ToString) ' Could stream the result in chunks to a file
                      For i As Integer = 0 To UBound(BlobIn)
                      If BlobIn(i) <BlobOut(i) Then MsgBox("Mismatc h !")
                      Next
                      Connection.Clos e()

                      Assuming you have a Table1 table with a Blob field and no records. It alos
                      test that the fictionous writren blob is the same than the blob we read back
                      later. From there you should have a starting point allowing to do what you
                      want...

                      ---

                      Patrice


                      "Patrice" <http://www.chez.com/scribe/a écrit dans le message de news:
                      eTGbOYGbHHA.340 8@TK2MSFTNGP03. phx.gbl...
                      Ok the error message is actually misleading as the method name is not
                      correct. This is actually in the insertion statement where you use to
                      string that dumps the type name inside the SQL statement resulting in a
                      SQL syntax error...
                      >
                      Please post a message if you solve the problem. I'll try to post a sample
                      later today so that you can go back on better tracks...
                      ----
                      Patrice
                      >
                      "Webbyz" <mail.webb@gmai l.coma écrit dans le message de news:
                      1174499501.1206 35.254940@d57g2 00...legr oups.com...
                      >Well at this point the GetData is gathering the Data and passing it
                      >into the Db.
                      >>
                      >Here is the Module for that function.
                      >>
                      >>
                      >Module DataAccessModul e
                      > Friend Function GetData(ByVal strSQL As String, ByVal TableName As
                      >String) As DataSet
                      > Dim myDataset As DataSet = New DataSet
                      > ' Create our connection string
                      > Dim myConnString As OleDbConnection = New
                      >OleDbConnectio n("Provider=Mic rosoft.Jet.OLED B.4.0;Data Source=c:
                      >\db1.mdb")
                      > ' Create our command
                      > Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                      >myConnString )
                      > ' Create our data adapter
                      > Dim myDataAdapter As OleDbDataAdapte r = New OleDbDataAdapte r
                      > ' Assign the SQL command to the data adapter
                      > myDataAdapter.S electCommand = myCommand
                      > Try
                      > ' Fill the dataset with the data from our command
                      > myDataAdapter.F ill(myDataset, TableName)
                      > ' Return the dataset
                      > GetData = myDataset
                      > Catch ex As OleDbException
                      > MsgBox("An error was thrown by the ADO.Net component in
                      >the GetData function. Error Details: " & _
                      > vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                      > Debug.WriteLine ("SQL Statement: " & strSQL)
                      > GetData = Nothing
                      > Finally
                      > ' Close the connection and destroy our connection objects
                      > myCommand.Conne ction.Close()
                      > myDataset = Nothing
                      > myConnString = Nothing
                      > myCommand = Nothing
                      > myDataAdapter = Nothing
                      > End Try
                      > End Function
                      >>
                      > Friend Function UpdateData(ByVa l strSQL As String) As Integer
                      > ' Create our connection string
                      > Dim myConnString As OleDbConnection = New
                      >OleDbConnectio n("Provider=Mic rosoft.Jet.OLED B.4.0;Data Source=c:
                      >\db1.mdb")
                      > ' Create our command
                      > Dim myCommand As OleDbCommand = New OleDbCommand(st rSQL,
                      >myConnString )
                      > Try
                      > ' Open the connection
                      > myCommand.Conne ction.Open()
                      > ' execute the SQL
                      > UpdateData = myCommand.Execu teNonQuery()
                      > Catch ex As OleDbException
                      > MsgBox("An error was thrown by the ADO.Net component in
                      >the GetData function. Error Details: " & _
                      > vbCr & ex.Message, MsgBoxStyle.Exc lamation, "Error!")
                      > Debug.WriteLine ("SQL Statement: " & strSQL)
                      > UpdateData = Nothing
                      > Finally
                      > ' Close the connection and destroy our connection object
                      > myCommand.Conne ction.Close()
                      > myConnString = Nothing
                      > myCommand = Nothing
                      > End Try
                      > End Function
                      >>
                      >End Module
                      >>
                      >
                      >

                      Comment

                      Working...