Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    FileItem and SmartGWT Enterprise

    I'm using the 'sql' serverType.

    How do I upload a file without storing it in the database using the FileItem control?

    I want to upload a file, save it on the server and store the filename in a database field.

    The doc says:
    To use Smart GWT's client-server upload system, you use a DataSource field of 'type' "binary".
    Now, if I set a field like this ISC server will expect to find a BLOB field in my table which obviously doesn't exist.

    If I don't set one, I get nothing in the dsRequest.getUploadedFile()

    Any thoughts? Thank you!

    #2
    Hi bftanase,

    Use a field of type "binary", but write a DMI for the upload operation that grabs the uploaded file via getUploadedFile() and stores it however you want, allowing the normal SQL operation to complete after this.

    Comment


      #3
      Hi,

      I was thinking things could work like this:

      - call saveData() on the form (with other fields besides FileItem)
      - hook into DSRequest (either DMI or subclassing IDACall)
      - get access to the uploaded file, dsRequest.getUploadedFile()
      - do java I/O with the file
      - dsRequest.setFieldValue("attachedFileName", fileName);
      - resume normal DS operation (I expect the server will write all the submited fields, including attachedFileName to the database).

      Problems:
      1) if I define a "binary" field in the ds.xml file as soon as I make a fetch operation I get a "Unknown column 'binaryColumn' in 'field list'" error. Any way of defining some sort of "virtual" field? A field that actually does not exist in the database?

      2) By using DMI from what I understand after studying the samples & docs I will have to create a class/method to replace the entire add/update operation, meaning I'll have to write code to manage the insert/update into the database. Is this assumption correct?

      I'm pretty sure I'm missing something here. Can you please clarify for me?

      Comment


        #4
        1) you can set customSQL=true on the field to avoid any SQL being generated for it.

        2) once you've modified the dsRequest, call execute() on it to execute the default logic (a SQL add/update in this case) then just return the resulting DSResponse.

        Comment


          #5
          I'm seeing something weird here:

          in my ds.xml file I've got this field for upload:

          Code:
          <field name="uploadedFile" type="binary" customSQL="true"></field>
          Now, with the first fetch the SQL generated is this:

          Code:
          Query: SELECT param.paramId, [...] param.uploadedFile_filename,  param.uploadedFile_filesize, param.uploadedFile_date_created,  FROM param WHERE (param.partId=2) LIMIT 0, 75
          How do I get rid of those _filename, _filesize, _date_created fields?

          UPDATE: I've created those fields in my table just too see if it works like this. However it looks like the server is trying to prepare a binary stream (for updating probably) and throws an index out of bounds exception

          the DMI:

          Code:
          public class UploadManager {
            public DSResponse update(DSRequest dSRequest, ServletContext context) throws Exception{
              if (dSRequest.getUploadedFile("uploadedFile")!=null){
                // i just want to log the filename at this point
                String fileName = dSRequest.getUploadedFile("uploadedFile").getShortFileName();
                System.out.println("File name: " + fileName);
          
              }
              return dSRequest.execute();
            }
          }

          and stacktrace

          Code:
          INFO: === 2010-03-11 11:57:00,653 [-(2)] INFO  SQLDataSource - [builtinApplication.paramMMR_update] Performing update operation with
                  criteria: {paramId:1}        values: {uploadedFile:"wallpaper-colosseum.jpg",paramDesc:"gdfg",paramId:1,partId:1,paramName:"sfdsf",paramTolerance:"gfdsg",paramStdVal:"fsdgdf",_selection_4:true,uploadedFile_filename:"wallpaper-colosseum.jpg",uploadedFile_filesize:1068216,uploadedFile_date_created:new Date(1268301417637)}
          
          INFO: === 2010-03-11 11:57:00,653 [-(2)] INFO  SQLValuesClause - [builtinApplication.paramMMR_update] Ignored data for non-existent columns: [_selection_4]
          
          INFO: === 2010-03-11 11:57:00,668 [-(2)] DEBUG PoolableSQLConnectionFactory - [builtinApplication.paramMMR_update] Returning unpooled Connection
          
          INFO: === 2010-03-11 11:57:01,653 [-(2)] INFO  SQLDriver - [builtinApplication.paramMMR_update] Executing SQL update on 'jdbc/receivingInspection': UPDATE param SET paramDesc='gdfg', uploadedFile_filename='wallpaper-colosseum.jpg', partId=1, uploadedFile_filesize=1068216, paramName='sfdsf', uploadedFile_date_created='2010-03-11 11:56:57.637', paramTolerance='gfdsg', paramStdVal='fsdgdf' WHERE (param.paramId=1)
          
          INFO: === 2010-03-11 11:57:02,653 [-(2)] DEBUG DataSourceDMI - Invocation threw exception
          java.lang.ArrayIndexOutOfBoundsException: 0
                  at com.mysql.jdbc.PreparedStatement.setBinaryStream(PreparedStatement.java:3185)
                  at com.sun.gjc.spi.base.PreparedStatementWrapper.setBinaryStream(PreparedStatementWrapper.java:366)
                  at com.isomorphic.sql.SQLDriver.doUpdate(SQLDriver.java:431)
                  at com.isomorphic.sql.SQLDriver.update(SQLDriver.java:400)
                  at com.isomorphic.sql.SQLDriver.executeUpdate(SQLDriver.java:495)
                  at com.isomorphic.sql.SQLDataSource.executeNativeUpdate(SQLDataSource.java:313)
                  at com.isomorphic.sql.SQLDataSource.SQLExecute(SQLDataSource.java:1139)
                  at com.isomorphic.sql.SQLDataSource.execute(SQLDataSource.java:227)
                  at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:721)
                  at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:658)
                  at com.isomorphic.application.AppBase.execute(AppBase.java:491)
                  at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1250)
                  at controller.UploadManager.fetch(UploadManager.java:22)
          I'm really close to solving this. I just need to make the ISC Server to ignore the default behavior of trying to write a binary field to the database.
          Last edited by bftanase; 11 Mar 2010, 02:13.

          Comment


            #6
            Apparently I solved this by setting dsRequest.setUploadedFiles(null) just before calling dsRequest.execute().

            It doesn't look like a supported API though...

            Comment


              #7
              Those fields (_filename et al) store metadata about the file so that you get the right filename if you subsequently download, and so that you can eg, show a list of all uploaded files and their sizes.

              customSQL="true" should have prevented any attempt to do SQL related to the binary file, we'll double check on this.

              setUploadedFiles() is not a documented API, but we'll add an API clearUploadedFiles() that has the effect you're looking for (even though not strictly needed with customSQL="true").

              Comment


                #8
                Ok. That would be nice!

                Thanks for looking into this.

                Comment


                  #9
                  Originally posted by Isomorphic
                  customSQL="true" should have prevented any attempt to do SQL related to the binary file, we'll double check on this.
                  That was my impression, but it doesn't seem to be the case. I'm sure I can work around it, but should I?

                  Comment


                    #10
                    You should find (with the latest nightlies) that customSQL now prevents binary insertion. However if you need to work with something older, use the setUploadedFiles(null) approach mentioned earlier in this thread.

                    Comment

                    Working...
                    X