Announcement

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

    Getting corrupt byte array

    Hi isomorphic,

    I am uploading PDF file from the client side using FileItem(also tried with UploadItem) and retrieving file at the server end using "dsRequest.getUploadedFiles()" api. I receive the byte array using "dsRequest.getUploadedFiles().get(0).get()" and then when i try to create the PDF again from this byte array but the file always seems to be corrupt. I compared the original and created PDF files content by opening the file using notepad, both were different. Some of the characters have been replaced with "�". Please suggest.

    Thanks.

    #2
    Does this have something to do with our software? It seems like you’re saying you get the expected byte array from us, but then some later step you’re doing (file IO maybe) you’ve mishandled the bytes and creating a problem (perhaps character encoding).

    If so, you want to look on a general Java forum for help, as this is not a problem with SmartClient.

    Comment


      #3
      No, i am saying that the byte array i am receiving from client side using "dsRequest.getUploadedFiles()" is itself corrupt. Then i try to create the pdf file, but since the byte stream is itself corrupt so the file created is also corrupt.

      Comment


        #4
        Sorry, that doesn't make any sense. Our own feature to store files in a DB uses the same API and same byte stream you receive. Whatever you are doing to turn the stream into a PDF file is going to be where the problem is.

        And, just to remind you, if you need help writing correct code to turn a stream into a file, that's a general Java question, not a SmartClient question, so please use other forums.

        Comment


          #5
          I have one question:
          Can we submit a form having attachment and certain other fields in the same save data call.? There is no issue in that, isn't it?

          Comment


            #6
            No issue, I have the same (a title for the uploaded file), see the sample here.

            Comment


              #7
              I have the following client side and server side code. Please see if anything seems wrong.

              ///////Client Side code/////////

              final String csrfCookie = Cookies.getCookie("XSRF-TOKEN");
              RPCManager.setActionURL(GWT.getHostPageBaseURL() + "orderdetail/submit-answer"
              + "?_csrf=" + csrfCookie);

              final DataSource submitAnswerDataSource = DataSource.get("QuestionSubmit");
              submitAnswerForm = new DynamicForm();
              submitAnswerForm.setDataSource(submitAnswerDataSource);
              submitAnswerForm.setIsGroup(true);
              submitAnswerForm.setGroupTitle("View Question(s) and submit answer");
              submitAnswerForm.setShowErrorText(true);
              submitAnswerForm.setErrorOrientation(FormErrorOrientation.BOTTOM);
              submitAnswerForm.setAutoFocus(true);
              submitAnswerForm.setID("order");
              submitAnswerForm.setBackgroundColor("white");
              submitAnswerForm.setAutoFetchData(false);


              final UploadItem uploadItem = new UploadItem("attachments", "dummyTitle");
              uploadItem.setRequired(true);
              uploadItem.setType("binary");

              final ButtonItem submit = new ButtonItem();
              submit.setTitle("Submit");

              submitAnswerForm.setFields(uploadItem, submit);

              submit.addClickHandler(new ClickHandler() {
              submit.addClickHandler(new ClickHandler() {
              public void onClick(final ClickEvent event) {
              submitAnswerForm.saveData(new DSCallback() {

              @Override
              public void execute(final DSResponse dsResponse, final Object data,
              final DSRequest dsRequest) {
              //Do some operation such as show error on error response etc.
              }
              });
              }
              }

              #################################
              QuestionSubmit.ds.xml

              <DataSource ID="QuestionSubmit"
              schemaBean="com.myCode.server.dto.QuestionAnswerInfoDTO">
              <fields>
              <field name="attachments" type="binary" title="file"/>
              </fields>
              </DataSource>

              #################################
              QuestionAnswerInfoDTO.java

              package com.myCode.server.dto;
              import java.io.Serializable;

              public class QuestionAnswerInfoDTO implements Serializable {
              private static final long serialVersionUID = 1L;

              private byte[] attachments;

              public byte[] getAttachments() {
              return attachments;
              }

              public void setAttachments(byte[] attachments) {
              this.attachments = attachments;
              }
              }

              Server side code
              ##################################
              MyController.java

              package com.myCode.server.web.controller;

              import java.util.Iterator;
              import java.util.logging.Logger;

              import javax.servlet.http.HttpServletRequest;
              import javax.servlet.http.HttpServletResponse;

              import org.springframework.stereotype.Controller;
              import org.springframework.web.bind.annotation.RequestMapping;
              import org.springframework.web.bind.annotation.RequestMethod;
              import org.springframework.web.servlet.ModelAndView;

              import com.isomorphic.datasource.DSRequest;
              import com.isomorphic.datasource.DSResponse;
              import com.isomorphic.rpc.RPCManager;
              import com.isomorphic.rpc.RPCRequest;

              @Controller
              @RequestMapping("/orderdetail")
              public class MyController extends BaseController{

              @SuppressWarnings("rawtypes")
              @RequestMapping(value = { "/submit-answer" }, method = { RequestMethod.POST })
              @RequestMapping(method = RequestMethod.POST)
              public ModelAndView handleQuesAnsRequest(final HttpServletRequest request, final HttpServletResponse response, final Locale locale) throws Exception {
              RPCManager rpc;
              boolean isSuccess = true;
              String errorMesage = null;
              final String[] params = new String[2];
              try {
              rpc = new RPCManager(request, response);
              }
              catch (final Exception exception) {
              exception.printStackTrace();
              }

              for (final Iterator i = rpc.getRequests().iterator(); i.hasNext();) {
              final Object req = i.next();
              if (req instanceof RPCRequest)
              throw new Exception("RPC call expects only DSRequests");
              final DSRequest dsRequest = (DSRequest) req;
              final String dsName = dsRequest.getDataSourceName();
              LOG.debug("-DataSource Name is { " + dsName + " }");

              final DSResponse dsResponse = new DSResponse();
              dsResponse.setSuccess();
              ISCFileItem files = (ISCFileItem) dsRequest.getUploadedFiles().get(0);
              File someFile = new File("someSystemLocation/somepdf.pdf");
              OutputStream fos = new FileOutputStream(someFile);
              fos.write(files.get());
              fos.flush();
              }
              }
              }

              Comment


                #8
                Your server code makes potentially invalid assumptions about character encoding that would corrupt the uploaded file. Please see any Java site or reference on character encoding.

                Comment


                  #9
                  Can i encode an UploadItem content to base64 before submitting to the server i.e before save data call of the the form.

                  Comment


                    #10
                    Hi sidharth1917,

                    pretty sure this is not the way to go (and the problem is an encoding mismatch serverside), but you could perhaps (don't know) use this DataSource-overload with a RequestTransformer.

                    Best regards
                    Blama

                    Comment

                    Working...
                    X