lufthansa747
lufthansa747

Reputation: 2061

GWT Send Image To Save On Server

I am creating a contact address book and would like to add the functionality for a user to upload a contacts picture and save it to the server. Below is the client side code

public class FileUploadDialog extends Composite 
{
    private VerticalPanel panel = new VerticalPanel();
    private DecoratorPanel dPan = new DecoratorPanel();

    public FileUploadDialog()
    {

        initWidget(dPan);
        dPan.setWidget(panel);

        // create a FormPanel
        //final FormPanel form = new FormPanel();
        // create a file upload widget
        final FileUpload fileUpload = new FileUpload();
        // create labels
        Label selectLabel = new Label("Select a file:");
        // create upload button
        Button uploadButton = new Button("Upload File");

        // add a label
        panel.add(selectLabel);
        // add fileUpload widget
        panel.add(fileUpload);
        // add a button to upload the file
        panel.add(uploadButton);

        uploadButton.addClickHandler(new ClickHandler() 
        {
            @Override
            public void onClick(ClickEvent event) {
                // get the filename to be uploaded
                String filename = fileUpload.getFilename();
                if (filename.length() == 0) {
                    Window.alert("No File Specified!");
                } else 
                {
                    System.out.println("submited");
                }
            }
        });         
    }
}

My question is how can i send the image file to the server using an rpc call?

Upvotes: 0

Views: 563

Answers (1)

nwilde97
nwilde97

Reputation: 111

If you want to use RPC you'll first need to get the contents of the image as a byte[] and then send it to the server. This can only be done if the target browser implements the FileAPI. There's a couple libraries that expose the FileAPI such as lib-gtw-file or gwt-file-api. Alternatively you could use JSNI (which is all the libraries are doing) to get the contents of the file.

If you are targeting browsers that don't support HTML5 then you can easily do a simple form submit with a parseable response. It's probably a better way than through RPC anyway.

Upvotes: 1

Related Questions