Friday, June 29, 2012

Re: Download file from server to client - w Servlet etc PLEASE?

A word of warning with downloading using Window.open, it won't work for IE8 if your site is running over SSL.  <a href...> works fine.

On Saturday, 13 September 2008 08:48:18 UTC+10, Joe Cole wrote:
Make sure you use the gwt jsni equivalent to;

var win = Window.open(url, name, options);
if ( win ) return true;
return false;

Otherwise you can't detect when popups are blocked. It will save you
tons of time in user support if you tell them to enable popups if the
window wasn't opened. It amazed me how many users didn't know how to
enable this.

Joe

On Sep 12, 8:33 pm, Jason Morris <lem...@gmail.com> wrote:
> I assume what you want is for the client to have a new file on their hard-drive.
> First you'll need a servlet that produces the data. I'm not sure what
> data-format you want to work with, so I'm gonna assume a plain text file here
> (note, this is all typed directly into my mail client, sorry for any mistakes).
>
> public class MyFileServlet extends HttpServlet {
>         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
> ServletException, IOException {
>
>                 resp.setContentType("text/plain");
>                 resp.setHeader("Content-Disposition", "attachment; filename=output.txt");
>
>                 PrintWriter out = resp.getWriter();
>                 out.println("This is the output content");
>                 out.println("Probably something dynamic should go in here");
>         }
>
> }
>
> Then you'll want to write the client side to fetch the file.
>
> public class MyEntryPoint implements EntryPoint {
>         public void onModuleLoad() {
>                 String link = GWT.getModuleBaseURL() + "servlet/myfiledownload";
>                 RootPanel.get().add(new HTML("<a href=\"" + link + "\">Download File</a>"));
>         }
>
> }
>
> You can also use Window.open(link, "downloadWindow", "");
> to download the file from an EventListener.
>
> Finally you'll need to configure the servlet in either your Module.gwt.xml file
> (for hosted mode), or in your web.xml file for web mode.
>
> Module.gwt.xml example, add:
>
> <servlet path="/servlet/myfiledownload"
> class="your.package.name.here.MyFileServlet" />
>
> web.xml add:
>
> <servlet>
>         <servlet-name>MyFileServlet</servlet-name>
>         <servlet-class>your.package.name.here.MyFileServlet</servlet-class>
> </servlet>
>
> <servlet-mapping>
>         <servlet-name>MyFileServlet</servlet-name>
>         <url-pattern>/your.package.name.here/servlet/myfiledownload</url-pattern>
> </servlet-mapping>
>
> Like I show in the web.xml example, you'll need to make sure that the servlet is
> bound to the module base directory (where the nocache.html files all live), and
> not next to the host HTML page. Another important factor is: the Servlet must
> not be in your "client" package, since the GWT compiler shouldn't get hold of it.
>
> Hope this helps.
> Jason.
>
> JohnnyGWT wrote:
> > I've seen several discussions on how to download a file to the client.
> > All contain bits of code but no complete examples.
>
> > FileUpload is fine & easy using Apache commons stuff.
>
> > Can someone PLEASE provide some examples etc for downloading a file to
> > the client?
> > In my scenario I have to send a newly created file to the client.
> > Either this is by a download servlet 'get' method or a URL.
>
> > Any full examples would be greatly appreciated.
>
> > Thanx in advance

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/qXl7QTOMA7cJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment