Announcement

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

    Upload images with rest client

    Hi,

    I'm developing a client to add new rows to my DB by using the smartgwt rest api.

    Mi rest client is created with apache httpclient.

    So far i've been able to post data to the datasource. This data consists only in text and numbers, but now i need to upload an image to the datasource.

    I've added the image field to the datasource and try to send a multipart form data with the apache httpclient but is not working (smartgwt doesn't recognize any of the fields in the multipart form).

    Witch one is the best way to upload images to smartgwt using rest methods?

    Thank you for your help.

    My smartgwt version is 4.1-p20140305

    #2
    The RestHandler servlet doesn't have support for file upload, but you can create a servlet that handles a standard form post and use the server-side DataSource APIs to save to the binary field. Just put an InputStream into the values of the DSRequest and execute() it.

    Comment


      #3
      Originally posted by Isomorphic View Post
      The RestHandler servlet doesn't have support for file upload, but you can create a servlet that handles a standard form post and use the server-side DataSource APIs to save to the binary field. Just put an InputStream into the values of the DSRequest and execute() it.
      Thank you, that information is what i needed to know. Then i'm going to create that servlet to handle the multipart form and use the DSRequest to insert the image.

      Comment


        #4
        Originally posted by Isomorphic View Post
        The RestHandler servlet doesn't have support for file upload
        Hello, I just wanted to know if file upload is still not supported

        Comment


          #5
          Hi claudiobosticco,

          I got this working like this in a normal DMI:

          Code:
                  <operationBinding operationType="custom" operationId="uploadAttachment" serverMethod="uploadAttachment" apiAllowed="true">
                      <explanation>Request must contains: title, file name, data size, data, and lead id.  Adds all this data to the appropriate data source.</explanation>
                  </operationBinding>
          Code:
              public DSResponse uploadAttachment(DSRequest dsRequest, HttpServletRequest servletRequest) throws Exception {
                  Map<String, Object> map = dsRequest.getValues();
          
                  Long leadId = (map.get(DatasourceFieldEnum.T_ATTACHMENT__LEAD_ID.getValue()) == null) ? null
                          : Long.parseLong(map.get(DatasourceFieldEnum.T_ATTACHMENT__LEAD_ID.getValue()).toString());
                  if (leadId == null)
                      return new DSResponse(dsRequest.getDataSource()).setFailure(I18n.getString("noLeadIdInRequest", servletRequest));
          
                  Object data = map.get(DatasourceFieldEnum.T_ATTACHMENT__DATA.getValue()) == null ? null
                          : map.get(DatasourceFieldEnum.T_ATTACHMENT__DATA.getValue());
                  if (data == null)
                      return new DSResponse(dsRequest.getDataSource()).setFailure(I18n.getString("noSourceDatasourceInRequest", servletRequest));
          
                  String fileName = map.get(DatasourceFieldEnum.T_ATTACHMENT__DATA_FILENAME.getValue()) == null ? null
                          : map.get(DatasourceFieldEnum.T_ATTACHMENT__DATA_FILENAME.getValue()).toString();
                  if (fileName == null)
                      return new DSResponse(dsRequest.getDataSource()).setFailure(I18n.getString("noFileNameInRequest", servletRequest));
          
                  String title = map.get(DatasourceFieldEnum.T_ATTACHMENT__TITLE.getValue()) == null ? null
                          : map.get(DatasourceFieldEnum.T_ATTACHMENT__TITLE.getValue()).toString();
                  if (title == null)
                      return new DSResponse(dsRequest.getDataSource()).setFailure(I18n.getString("noTitleInRequest", servletRequest));
          
                  // base64 is up to 4/3 of the original file size
                  byte[] decodedData = Base64.decodeBase64(data.toString().getBytes());
                  int dataSize = decodedData.length;
          
                  File file = new File(servletRequest.getRealPath("/ds/T_ATTACHMENT.ds.xml"));
                  final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                  Document doc = documentBuilder.parse(file);
          
                  XPathFactory xPathFactory = XPathFactory.newInstance();
                  XPath xpath = xPathFactory.newXPath();
                  XPathExpression xpathExpression = xpath.compile("//field[@name='DATA']");
          
                  NodeList nodeList = (NodeList) xpathExpression.evaluate(doc, XPathConstants.NODESET);
                  int maxSize = Integer.parseInt(nodeList.item(0).getAttributes().getNamedItem("maxFileSize").getNodeValue());
          
                  if (dataSize > maxSize)
                      return new DSResponse(dsRequest.getDataSource()).setFailure(I18n.getString("dataSize", servletRequest));
                  ByteArrayInputStream blob = new ByteArrayInputStream(decodedData);
          
                  Map<String, Object> attachmentMap = new HashMap<String, Object>();
                  attachmentMap.put(DatasourceFieldEnum.T_ATTACHMENT__TITLE.getValue(), title);
                  attachmentMap.put(DatasourceFieldEnum.T_ATTACHMENT__DATA_FILENAME.getValue(), fileName);
                  attachmentMap.put(DatasourceFieldEnum.T_ATTACHMENT__DATA_FILESIZE.getValue(), dataSize);
                  attachmentMap.put(DatasourceFieldEnum.T_ATTACHMENT__DATA.getValue(), blob);
                  attachmentMap.put(DatasourceFieldEnum.T_ATTACHMENT__LEAD_ID.getValue(), leadId);
          
                  DSRequest addRequest = new DSRequest(dsRequest.getDataSourceName(), DataSource.OP_ADD, dsRequest.getRPCManager());
                  Helper.prepareRequest(addRequest, dsRequest);
                  addRequest.setValues(attachmentMap);
          
                  DSResponse addResponse = addRequest.execute();
          You'll have a pretty big HTTP POST with base64 data in one field.

          Best regards
          Blama

          Comment


            #6
            For anyone finding this thread via search: This one regarding binary downloads via RESTHandler might be related.

            Comment

            Working...
            X