Home » Eclipse Projects » Remote Application Platform (RAP) » Display or download files from server.(The browser attemps to display the file within the browser instead of opening a Dialog to choose between opening or saving the file.)
Display or download files from server. [message #657553] |
Thu, 03 March 2011 04:27  |
Eclipse User |
|
|
|
Hello All.
In rap 1.3.1, following the example found here http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link .3F , I'm trying to implement an object that would enable the user to get a file from the server and either open it in a system editor or save it on the local file system.
I must have missed something because, even when I hard code the content type to "application/pdf" for instance while trying to get a pdf file, the browser (FireFox 3.6 on CentOS 5.5) attempts to directly display it and it results to an error :
Could not evaluate javascript response:
SyntaxError: syntax error
%PDF-1.4
...
Normally, while browsing the net, when I click on a link to a pdf file (http://www.samplepdf.com/sample.pdf for instance), I get a dialog box that ask me weither I want to download it or open it with my system editor.
I would like to have the same behaviour within my app.
Here is the code I use to begin with and understand the process :
...
HttpServletResponse response = RWT.getResponse();
response.setContentType("application/pdf");
byte[] ba = null;
ba = FileUtils.readFileToByteArray(tmpFile);
response.setContentLength(ba.length);
String contentDisposition = "attachment; filename=\"" + fileName + "\"";
response.setHeader("Content-Disposition", contentDisposition);
response.getOutputStream().write(ba);
I'm sorry if my question is trivial but I have encountered pbs trying searching the net with both "download" or "display" key words .
Thanks for your help.
Bruno
[Updated on: Thu, 03 March 2011 04:49] by Moderator
|
|
| |
Re: Display or download files from server. [message #657975 is a reply to message #657954] |
Fri, 04 March 2011 13:53   |
Eclipse User |
|
|
|
Hi Sinou,
I see you have tried:
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
This works in our code.
Here is the download handler that we use:
public class DownloadHandler implements IServiceHandler
{
private File srcFile;
private URI bundleFile;
private String clientName;
private String contentType;
private String id;
private DownloadHandler(String id, String contentType, String clientFileName)
{
this.id = id.replaceAll("\\s", "%20");
this.clientName = clientFileName;
this.contentType = contentType;
RWT.getServiceManager().registerServiceHandler(id+hashCode(), this);
}
public DownloadHandler(String id, File wbFile, String contentType, String clientFileName)
{
this(id, contentType, clientFileName);
this.srcFile = wbFile;
}
public DownloadHandler(String id, URI bundleFile, String contentType, String clientFileName)
{
this(id, contentType, clientFileName);
this.bundleFile = bundleFile;
}
public String getURL()
{
StringBuffer url = new StringBuffer();
url.append(URLHelper.getURLString(false));
url.append("?");
url.append(IServiceHandler.REQUEST_PARAM);
url.append("=" + id+hashCode());
return ContextProvider.getResponse().encodeURL(url.toString());
}
public void service() throws IOException, ServletException
{
HttpServletResponse response = ContextProvider.getResponse();
response.setContentType(contentType);
if (Boolean.valueOf(ContextProvider.getRequest().getParameter("attachment"))) {
response.setHeader("Content-Disposition", "attachment;filename=\"" + clientName + "\"");
}
ServletOutputStream outStream = response.getOutputStream();
InputStream srcStream = null;
if (srcFile != null) {
srcStream = new FileInputStream(srcFile);
if (srcFile.length() < Integer.MAX_VALUE) {
response.setContentLength((int)srcFile.length());
}
}
else if (bundleFile != null) {
srcStream = bundleFile.toURL().openStream();
}
try {
byte[] content = new byte[1024];
int bytesRead = 0;
while ((bytesRead = srcStream.read(content)) > 0) {
outStream.write(content, 0, bytesRead);
}
} catch (IOException e) {
Logger.alert(e.toString());
} finally {
srcStream.close();
}
}
public void dispose()
{
RWT.getServiceManager().unregisterServiceHandler(id+hashCode());
}
}
[Updated on: Fri, 04 March 2011 13:58] by Moderator
|
|
| | | |
Re: Display or download files from server. [message #658804 is a reply to message #658714] |
Wed, 09 March 2011 15:37   |
Eclipse User |
|
|
|
Bruno,
We have a similar interaction in our system. Users can either right-click and select an menu item or double-click to download a file. We wrapped this functionality up into a "DownloadDialog" which creates a hidden dialog with a Browser widget (corresponding to an iframe on the client), and sets the URL of the download handler into it. The download handler specifies the Content-Disposition HTTP header to tell the user's browser the file should be downloaded.
This works well in most cases, but Internet Explorer's default settings block javascript-initiated file downloads (which is what ultimately happens). When this happens the user is presented the "information bar" which slides down from the top of the browser. Unless their security settings prevent it, they can choose to always allow downloads from your website. This will cause the page to reload which will restart the workbench and they will probably have to find their way back to where they were. They should only have to do this once though. I think there are ways to lock IE down so users cannot change this setting, so it may not work for all users if they are in a restricted environment.
This issue with IE is the reason that the recommended way is to have a link the user must click. If you are ok with the caveats, then you can make it work with a double-click in a viewer.
I have included the source for both the DownloadDialog and DownloadHandler we use below.
Hope this helps,
Cole
DownloadDialog:
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
public class DownloadDialog extends Dialog {
Browser b;
String url;
public DownloadDialog(Shell parent) {
super(parent);
}
public void setURL (String url) {
if (b != null && !b.isDisposed()) {
b.setUrl(url);
}
this.url = url;
}
@Override
protected Control createDialogArea(Composite parent) {
Control control = super.createDialogArea(parent);
b = new Browser(parent, SWT.NONE);
if (url != null) {
b.setUrl(url);
}
return control;
}
@Override
protected Control createButtonBar(Composite parent) {
return null;
}
@Override
protected int getShellStyle() {
return SWT.NO_TRIM;
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setSize(1, 1);
newShell.setMinimized(true);
}
}
DownloadHandler:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.rwt.RWT;
import org.eclipse.rwt.internal.service.ContextProvider;
import org.eclipse.rwt.internal.util.URLHelper;
import org.eclipse.rwt.service.IServiceHandler;
public class DownloadHandler implements IServiceHandler
{
private File srcFile;
private URI bundleFile;
private String clientName;
private String contentType;
private String id;
private DownloadHandler(String id, String contentType, String clientFileName)
{
this.id = id.replaceAll("\\s", "%20");
this.clientName = clientFileName;
this.contentType = contentType;
RWT.getServiceManager().registerServiceHandler(id+hashCode(), this);
}
public DownloadHandler(String id, File wbFile, String contentType, String clientFileName)
{
this(id, contentType, clientFileName);
this.srcFile = wbFile;
}
public DownloadHandler(String id, URI bundleFile, String contentType, String clientFileName)
{
this(id, contentType, clientFileName);
this.bundleFile = bundleFile;
}
public String getURL()
{
StringBuffer url = new StringBuffer();
url.append(URLHelper.getURLString(false));
url.append("?");
url.append(IServiceHandler.REQUEST_PARAM);
url.append("=" + id+hashCode());
return ContextProvider.getResponse().encodeURL(url.toString());
}
public void service() throws IOException, ServletException
{
HttpServletResponse response = ContextProvider.getResponse();
response.setContentType(contentType);
if (Boolean.valueOf(ContextProvider.getRequest().getParameter("attachment"))) {
response.setHeader("Content-Disposition", "attachment;filename=\"" + clientName + "\"");
}
ServletOutputStream outStream = response.getOutputStream();
InputStream srcStream = null;
if (srcFile != null) {
srcStream = new FileInputStream(srcFile);
if (srcFile.length() < Integer.MAX_VALUE) {
response.setContentLength((int)srcFile.length());
}
}
else if (bundleFile != null) {
srcStream = bundleFile.toURL().openStream();
}
try {
byte[] content = new byte[1024];
int bytesRead = 0;
while ((bytesRead = srcStream.read(content)) > 0) {
outStream.write(content, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
srcStream.close();
}
}
public void dispose()
{
RWT.getServiceManager().unregisterServiceHandler(id+hashCode());
}
}
|
|
|
Re: Display or download files from server. [message #658822 is a reply to message #658804] |
Wed, 09 March 2011 17:33  |
Eclipse User |
|
|
|
Thanks a lot Cole.
On my side I finally achieve it quite simply adding the following snippet in my double click listener
URL url = new URL(createFullDownloadUrl(fileName, fileId));
PlatformUI.getWorkbench().getBrowserSupport().createBrowser("DownloadDialog").openURL(url);
...
private String createFullDownloadUrl(String fileName, String fileId) {
StringBuilder url = new StringBuilder();
url.append(RWT.getRequest().getRequestURL());
url.append(createParamUrl(fileName, fileId));
return url.toString();
}
private String createParamUrl(String filename, String fileId) {
StringBuilder url = new StringBuilder();
url.append("?");
url.append(IServiceHandler.REQUEST_PARAM);
url.append("=downloadServiceHandler");
url.append("&filename=");
url.append(filename);
url.append("&fileid=");
url.append(fileId);
String encodedURL = RWT.getResponse().encodeURL(url.toString());
return encodedURL;
}
the only trick is that I have only one serviceandler that must have a reference to some file provider to recover file from Ids.
Anyway, I then fall against a problem very similar to yours : the dialog box is open in a new "pop up" browser that is blocked by default...
But I consider my problem as solved for now, I will try to find a workaround maybe later on.
I'll try to post the full code soon if it can help someone else, but not tonight ...
|
|
|
Goto Forum:
Current Time: Sun Jul 27 02:19:50 EDT 2025
Powered by FUDForum. Page generated in 0.04533 seconds
|