Is it possible to download a file from server to m

2019-02-20 21:48发布

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);
});
  }-*/;

2条回答
一纸荒年 Trace。
2楼-- · 2019-02-20 22:06

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.

查看更多
迷人小祖宗
3楼-- · 2019-02-20 22:14

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);
            }
    }-*/;
    
查看更多
登录 后发表回答