0

I am not able to upload excel file and parse using GWT 2.7.0. Referred many links Link1 Link2

using above technique and RequestBuilder I couldn't send parsed excel data back to client. Finally implemented GWT RPC technique but having problem load Excel file as GWT cannot implement File.io api on client(Javascript or browser cannot read)

Code:

Client side FileUploading

public class MyFileUpload extends Composite implements Constants{

private ExcelClientServiceImpl excelServiceClient;
private VerticalPanel vPanel;
public MyFileUpload(ExcelClientServiceImpl excelServiceClient){
    this.excelServiceClient = excelServiceClient;
    this.vPanel = new VerticalPanel();
    initWidget(this.vPanel);
}



public void initiateUpload() {
    // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();
    form.setAction("/excelParser");

    // Because we're going to add a FileUpload widget, we'll need to set the
    // form to use the POST method, and multipart MIME encoding.
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

    // Create a panel to hold all of the form widgets.
    vPanel = new VerticalPanel();
    form.setWidget(vPanel);

    // Create a FileUpload widget.
    final FileUpload upload = new FileUpload();
    //upload.setName("uploadFormElement");
    vPanel.add(upload);
    System.out.println("File name is : "+upload.getFilename());

    // Add a 'submit' button.
    vPanel.add(new Button("Submit", new ClickHandler() {
      public void onClick(ClickEvent event) {
        Window.alert("In Button >>>>>> "+event);

        form.submit();
      }
    }));

    // Add an event handler to the form.
    form.addSubmitHandler(new FormPanel.SubmitHandler() {
      public void onSubmit(SubmitEvent event) {
        Window.alert("In Handler >>>>>> "+event);
      }
    });
    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
      public void onSubmitComplete(SubmitCompleteEvent event) {
        // When the form submission is successfully completed, this event is
        // fired. Assuming the service returned a response of type text/html,
        // we can get the result text here (see the FormPanel documentation for
        // further explanation).
        Window.alert("In complete >>>>>> "+event.getResults());
      }
    });

    RootPanel.get().add(form);
  }

}

Client Interface

public interface ExcelClientServiceInt {
void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
        String separator);
void readingExcel();

}

Client Implementation

public class ExcelClientServiceImpl implements ExcelClientServiceInt{

private ExcelServiceIntAsync service; 
private MyFileUpload excelUpload;

public ExcelClientServiceImpl(String url){
    this.service = GWT.create(ExcelParserService.class);
    ServiceDefTarget endpoint = (ServiceDefTarget) this.service;
    endpoint.setServiceEntryPoint(url);
    this.excelUpload = new MyFileUpload(this);
}

@Override
public void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
        String separator) {
    this.service.parse(fileName, lines, integerNumber, floatNumber, separator, new DefaultCallback());

}

@Override
public void readingExcel() {

    this.service.readingExcel(null, new DefaultCallback());

}


private class DefaultCallback implements AsyncCallback{

    @Override
    public void onFailure(Throwable caught) {
        System.out.println("Output failed");

    }

    @Override
    public void onSuccess(Object result) {
        System.out.println("Output reieved successfully "+result);

    }

}

}

Service Interface

@RemoteServiceRelativePath("excelParser")

public interface ExcelServiceInt extends RemoteService{ void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber, String separator); public List readingExcel(String fileName); }

Async Call Back

public interface ExcelServiceIntAsync{
void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
        String separator, AsyncCallback<Void> callBack);
void readingExcel(String fileName, AsyncCallback<List<String>> callBack);

}

Server side Service

public class ExcelParserService extends RemoteServiceServlet implements ExcelServiceInt{


public void parse(String fileName, String[] lines, StringBuilder integerNumber, StringBuilder floatNumber,
        String separator) {
    ExcelParser.parse(fileName, lines, integerNumber, floatNumber, separator);

}

public List<String> readingExcel(String fileName) {

    return ExcelParser.readingExcel(fileName);

}

}

atul
  • 21
  • 6
  • No idea what you actually want . Do you want to make file upload work? do you want to send data using GWT RPC? or do you want to read a file on the client side to send its text content using GWT RPC? (don't even ask for doubts in the server) – Ignacio Baca Feb 14 '18 at 07:30
  • Here there is an example to load the content of a file on the client side (https://github.com/ibaca/dndfiles-gwt) but it probably doesn't work in old browsers. – Ignacio Baca Feb 14 '18 at 07:32
  • I want to upload file using gwt 2.7.0 then have to parse the uploaded file on server as gwt is for browser and file handling is not possible on browser then after parsing send response as string. I have to use gwt rpc as servlet can't send back the response to client or gwt can't read the response, I tried request builder and form.submit form.addcompletesubmit(). Hope it is clear now!! – atul Feb 14 '18 at 14:33
  • I am able to implement file parsing and sending string response. But does someone know how to read local disk file path using GWT. If I hard code file path it works. – atul Feb 14 '18 at 15:46
  • What about the drag and drop example? It shows the content of the file and is a client only example. – Ignacio Baca Feb 14 '18 at 16:17
  • This option came to my mind but I havent seen any example nor any gwt api support this feature. Please share the link if you know any. I will be thankful to you :) – atul Feb 15 '18 at 05:57
  • https://github.com/ibaca/dndfiles-gwt – Ignacio Baca Feb 15 '18 at 06:14

0 Answers0