Home » Eclipse Projects » Remote Application Platform (RAP) » How to open/download a file?
How to open/download a file? [message #1059826] |
Tue, 21 May 2013 18:27 |
Julia Kurde Messages: 91 Registered: November 2011 Location: Berlin, Germany |
Member |
|
|
Hello,
How can I open a file, e.g. a pdf, that is located in one plugin of the application?
I already have
ServiceManager manager = RWT.getServiceManager();
String urlString = URLHelper.getContextURLString();
urlString = urlString + manager.getServiceHandlerUrl(DownloadServiceHandler.ID);
System.out.println(urlString); // http://127.0.0.1:8080/jlzhview?servicehandler=download
IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench().getBrowserSupport();
try {
IWebBrowser browser = browserSupport.createBrowser(
IWorkbenchBrowserSupport.AS_EDITOR, "id", "name", "tooltip");
urlString = urlString + "&filename=" + "theFile.pdf";
browser.openURL(new URL(urlString));
} catch (PartInitException | MalformedURLException e) {
//some Exception handling...
}
The DownloadServiceHandler is registered when the application starts and it is pretty simple:
public class DownloadServiceHandler implements ServiceHandler {
public static final String ID = "download";
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String fileName = request.getParameter( "filename" );
String contentDisposition = "attachment; filename=\"" + fileName + "\"";
response.setHeader( "Content-Disposition", contentDisposition );
}
}
This will open a new Browser window with a message box asking if the file should be opened or saved. But that cannot work because the file path is missing (the file cannot be found)!
So how to define the path to the file in the URL? In the RCP I would get it like this:
URL root = MyPlugin.getDefault().getBundle().getEntry("/");
URL url = new URL(root, "/documentation/theFile.pdf");
url = FileLocator.resolve(url);
This is the absolute path of that file. But in RAP I need it relative to the servlet path, which I get from manager.getServiceHandlerUrl(String) and it will need some "&path=..." or something like that in the URL (I guess).
Or do I have to store files that can be downloaded in a special download folder on the server?
Thanks for your help!
Regards,
Julia
|
|
| | | |
Re: How to open/download a file? [message #1059981 is a reply to message #1059919] |
Wed, 22 May 2013 12:39 |
Julia Kurde Messages: 91 Registered: November 2011 Location: Berlin, Germany |
Member |
|
|
Thanks for your answers! I was a bit confused how to pass parameters to the ServiceHandler. Finally I did it like this:
public class PdfServiceHandler implements ServiceHandler {
public static final String ID = "pdf_download";
private static final String P1 = "p1";
private static final String P1 = "p2";
private static final String P3 = "p3";
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String p1String = request.getParameter(p1);
String p2String = request.getParameter(p2);
String p3String = request.getParameter(p3);
URL url = new URL(/* generate file URL using p1, p2, p3 */);
/* give some information about the file */
response.setContentType("application/pdf");
String contentDisposition = "attachment; filename=\"" + p1 + "\"";
response.setHeader("Content-Disposition", contentDisposition);
/* write the file content */
int i;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream inputStream = url.openConnection().getInputStream()) {
while ((i = inputStream.read()) != -1) {
baos.write(i);
}
final byte[] byteArray = baos.toByteArray();
response.setContentLength(byteArray.length);
try (ServletOutputStream outputStream = response.getOutputStream()) {
outputStream.write(byteArray);
}
}
}
/* generate the URL that is passed to the browser using some parameters */
public static String generateUrlString(String p1String, String p2String, String p3String) {
String urlString = URLHelper.getContextURLString();
urlString = urlString + RWT.getServiceManager().getServiceHandlerUrl(ID);
urlString = urlString +
"&" + P1 + "=" + p1String+
"&" + P2 + "=" + p2String+
"&" + P3 + "=" + p3String;
return urlString;
}
}
And in the action that is related to the download button:
IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = browserSupport.createBrowser(
IWorkbenchBrowserSupport.AS_EDITOR, "id", "name", "tooltip");
/* the actual values for the params are generated in the action */
String urlString = PdfServiceHandler.generateUrlString("param1", "param2", "param3");
browser.openURL(new URL(urlString));
This works fine so far! What I don't like so much is that the browser will open as a popup. Is it possible to change that behavior? Actually it would be best, if the pdf is opened directly in a new browser tab.
Thanks again!
Regards,
Julia
|
|
| | |
Re: How to open/download a file? [message #1060021 is a reply to message #1059993] |
Wed, 22 May 2013 14:47 |
Julia Kurde Messages: 91 Registered: November 2011 Location: Berlin, Germany |
Member |
|
|
Right, when I take out "attachment" from the "Content-Disposition", or don't set it at all, the file is opened directly, but still in a popup window. I tried IE 10, Chrome, Nightly (that's a 64 bit firefox) and the Eclipse internal browser (this one will need at least the file name).
And I don't see any effect when setting or not setting
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
response.setHeader("Cache-Control", "max-age=0");
The content type seems to be determined by the file content automatically (except Eclipse internal browser). Maybe that's different for older/other browsers. So setting it doesn't hurt.
What exactly means "Cache-Control"? The file will not change, so if it was downloaded once, it can be read from the cache without any problem.
So if I don't set "Cache-Control", the file will be cached?
|
|
|
Re: How to open/download a file? [message #1060066 is a reply to message #1060021] |
Wed, 22 May 2013 19:43 |
Markus rüger Messages: 369 Registered: July 2009 |
Senior Member |
|
|
Julia Kurde <forums-noreply@xxxxxxxx> wrote:
> Right, when I take out "attachment" from the "Content-Disposition", or
> don't set it at all, the file is opened directly, but still in a popup
> window. I tried IE 10, Chrome, Nightly (that's a 64 bit firefox) and the
> Eclipse internal browser (this one will need at least the file name).
> And I don't see any effect when setting or not setting
>
> response.setContentType("application/pdf");
> response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
> response.setHeader("Cache-Control", "max-age=0");
>
> The content type seems to be determined by the file content automatically
> (except Eclipse internal browser). Maybe that's different for older/other
> browsers. So setting it doesn't hurt.
> What exactly means "Cache-Control"? The file will not change, so if it
> was downloaded once, it can be read from the cache without any problem.
> So if I don't set "Cache-Control", the file will be cached?
Hi Julia
the browser decides how to open popups. You can change this in the settings
of your browser.
And if I remember correctly there is no way in changing this behavior.
Regards
Markus
|
|
| | |
Goto Forum:
Current Time: Thu Sep 26 12:56:51 GMT 2024
Powered by FUDForum. Page generated in 1.03737 seconds
|