Is it possible to download a file from server to my device having MGWT&GwtPhoneGap app?

*爱你&永不变心* 提交于 2019-12-04 02:21:42

问题


I developed an app using MGWT and GwtPhoneGap and I used rpc for server communication. I want to download a file from server to my client device(iOs/android).is this possible? if so what I need to do to achieve my goal?. I'm not getting great support from MGWT forums. Please tell me the way to do it.

thanks in Advance

I put some effort on my question, I've done like this. but still not succeeded. please go through my code.

public final native String download(String serverUrl,String filepath,Callback callback)
  /*-{
return this.download(serverUrl, filePath, function(result) {
callback.Callback::o:onSuccess(Result;);)(result);
}, function(error) {
 callback.Callback::o:onError(Error;);)(error);
});
  }-*/;

回答1:


If the file is a simple file (A PDF for exemple) Just put a link to the file URL, the mobile navigator will download the file to the phone. If the file is generated, you must create a servlet for doing the download :

public class ServletDownloadDemo extends HttpServlet{

  private static final int BYTES_DOWNLOAD = 1024;

  public void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws IOException{
    response.setContentType("text/plain");
    response.setHeader("Content-Disposition",
                     "attachment;filename=downloadname.txt");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/testing.txt");

    int read=0;
    byte[] bytes = new byte[BYTES_DOWNLOAD];
    OutputStream os = response.getOutputStream();

    while((read = is.read(bytes))!= -1){
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close(); 
   }
}

In GWT for a Post Request, you can use th FormPanel which will send the request to an hidden iframe.

FormPanel form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
form.setAction("/downloadServlet");
FlowPanel hiddenPanel = new FlowPanel();
hiddenPanel.add(new Hidden("name1", "value"));
hiddenPanel.add(new Hidden("name2", "value"));
form.setWidget(hiddenPanel);
RootPanel.get().add(form);
form.submit();

Or you can put a real form.

But with PhoneGap, you can use the File API, but you must ask to the user where write the file in his phone. You can download the file content via a RPC request if you want, and write to the file.




回答2:


In my case I just wrote a servlet which will provide the file.

From then on you have two choices:

  • Put a link in your app and redirect a user to the url which will be caught by the servlet.
  • For POST requests use a RequestBuilder (not hidden forms !), then write the response to a new window using JSNI.

    private native void openWindow(String contents, String blockedMsg) /*-{
            var win = window.open("", "_blank");
            if (win && win.top) {
                win.document.write(contents);
            } else {
                alert(blockedMsg);
            }
    }-*/;
    


来源:https://stackoverflow.com/questions/18888332/is-it-possible-to-download-a-file-from-server-to-my-device-having-mgwtgwtphoneg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!