Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to upload a file via GWT RPC Servlets?

is it possible to create a file upload with Googles GWT RPC mechanism? As now I'm using a simple HTTPServlet with a doPost Method which gets addressed from the form!

Is it (without the help of libs like gwtupload) possible to upload a XML file via GWTs FileUpload Widget to a RPC Service and work with the content of the file?

BR; mybecks

like image 362
mybecks Avatar asked Jan 17 '23 13:01

mybecks


2 Answers

Actually, there are two ways of uploading file with gwtupload as you mentioned and via gwt-rpc this one is a bit difficult due to browser security. For implementing with gwt-rpc you should override service(final HttpServletRequest request,HttpServletResponse response) method inside your service implementation which inherits RemoteServiceServlet. For client-side you should have code something like this:

final FormPanel formPanel = new FormPanel();
formPanel.setAction(GWT.getModuleBaseURL()+”fileUpload”);
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);

Inside service method you can get the file with using FileUpload, and don't forget registering fileupload url pattern in your web.xml and also @RemoteServiceRelativePath("path") in your service interface which inherits RemoteService. Good Luck!

like image 84
Jama A. Avatar answered Jan 31 '23 05:01

Jama A.


It is possible, but it is not so easy. First you will need to be able to read file from client code. Most of the browsers support FileReader API, but not all of them. So fo Internet Explorer you will have to use flash or some other plugin technology for accessing files.

Also by default there is no binding for File Reader API in GWT, so you will have to do it by yourself.

Next thing is that you are going to send files as Base64 encoded string over GWT -RPC and you'll have to decode on server-side. Because of this you might run into some memory problems if your users start uploading a lot of large files (around 20 mb).

like image 32
jusio Avatar answered Jan 31 '23 05:01

jusio