How to open a document using native tool.

12 views
Skip to first unread message

Jit

unread,
Oct 20, 2011, 12:37:58 PM10/20/11
to Google Web Toolkit
I am building a GWT based app. I have a requirement to open a document
using a native tool based on it's mime type. For example if the
document is PDF it should use Adobe Reader, If It is word document
file it should use MS word and so on. can I get some example codes or
some guidelines to do it ?

Thanks in advance for your help.

Thad

unread,
Oct 20, 2011, 5:26:30 PM10/20/11
to Google Web Toolkit
Write a servlet to get the file, set the HttpServletResponse content
type to the file's MIME, and write the file to the
ServletOutputStream.

You can determine the MIME type with the aid of J2EE javax.activation
package (see MimetypesFileTypeMap and FileDataSource). You may get
lucky... (see WARNING below).

In your GWT client, create a Frame widget (this is an IFRAME) and add
it to your page. You can set its visibility to false. Build your
servlet string like

String fileUrl = GWT.getModuleBaseURL() + "myGetFileServlet?...; //
Add GET params

Then call myFrame.setUrl(fileUrl). This will call your servlet with
the IFRAME as a target.

WARNING: Often browsers, especially IE, will assume they know better
what the file type is and ignore your MIME type. This means a JPEG or
PNG might open in your invisible IFRAME. In my servlet, I circumvent
this by always using a generic application MIME type:

String encName = URLEncoder.encode(fileName, "UTF-8");
String ua = request.getHeader("User-Agent").toLowerCase();
response.addHeader("Content-Disposition", "attachment; filename=\"" +
encName + "\"" );
if (ua.indexOf("msie") != -1) {
response.setContentType("application/force-download; name=\"" +
encName + "\"" );
}
else {
response.setContentType("application/octet-stream; name=\"" +
encName + "\"" );
}

Then the browser asks the what to do--save or open with.
Reply all
Reply to author
Forward
0 new messages