Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
getting files (pdf, excel, doc, gif, jpg, tif..) + outputstr Agustin Campos May 5, 2008 2:22 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Eric Devlin May 5, 2008 12:41 PM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 7, 2008 4:48 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Eric Devlin May 7, 2008 5:50 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 7, 2008 6:36 AM
Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi Ray Augé May 27, 2008 11:58 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Ray Augé May 8, 2008 7:03 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 8, 2008 7:13 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Ray Augé May 8, 2008 7:55 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 8, 2008 8:57 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 9, 2008 5:59 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Ray Augé May 9, 2008 8:07 AM
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Ivano Carrara May 10, 2008 9:24 AM
Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi Ray Augé May 27, 2008 11:58 AM
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel Rishi Dev Gupta December 8, 2008 10:35 PM
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel kameshwar singh January 7, 2009 10:18 AM
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel Vivek N Kapse March 20, 2009 3:09 PM
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel Minhchau Dang March 20, 2009 3:34 PM
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel Vivek N Kapse March 24, 2009 3:04 PM
[SOLVED] RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu Agustin Campos May 13, 2008 11:11 PM
Agustin Campos
getting files (pdf, excel, doc, gif, jpg, tif..) + outputstr
May 5, 2008 2:22 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

Hello all!!!!

I am trying to send data to res.OutputStream. These are the things I would like to do:
a) I would like to get a file from a database
b) I would like to build a file on the fly

Both cases are almost done.

#######
Case a):

I have a file stored on the database.
I get that file from there and send to a Blob object.
From there I put alll its data in a byte[] object.

Everything till here works.

Now, I write that byte[] object in the OutputStream:

res.OutputStream.write(my_byte[]_name);
res.OutputStream.flush();
res.OutputStream.close();


I change some headers. i.e:
res.setHeader("Content-Disposition", "attachment; filename=" + nombre);


When I ask to get this file my portlet gets broken because all data on that file is shown IN the portlet instead of getting the open-file window.

###############

Case b)

I manage to build the file on the fly. It is a PDF one, but it could be an excell one, doc one... etc, etc.
The case is that I manage to build it and store it on the hard disk.

I would like to send it to the OutputStream, as I would do with "Case a)", but I will have the same problem.


###############

Once I've told you my problem, my question is:

Is there any way to make that file opened with its application instead of getting it opened ON the portlet?

I would be greatfull if you could help me.

Thank you a lot,

Agustìn

P.D: When talking about files and databases in "Case a)", I must do it.
Eric Devlin
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 5, 2008 12:41 PM
Answer

Eric Devlin

Rank: Regular Member

Posts: 154

Join Date: February 19, 2008

Recent Posts

Hey,
If you are trying to get the results in another browser window, it sounds like you may need a servlet not a portlet unless the portlet is an iframe.

Eric
Agustin Campos
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 7, 2008 4:48 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

The problem is that Servlets doesn't work very well on my programs. They do extrange things.

The biggest problem is the res.OutputStream(), which is null.

Are there any more ideas?
Eric Devlin
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 7, 2008 5:50 AM
Answer

Eric Devlin

Rank: Regular Member

Posts: 154

Join Date: February 19, 2008

Recent Posts

Hey,
In the servlet, why don't you try setting the headers and content type before you get the output stream? What servlet instance are you using? What does your link look like <a href=/customservlet?filename=blah" target="_otherwindow">see it to believe it</a>
Eric
Agustin Campos
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 7, 2008 6:36 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

There are few thing I must say:
- What is fails is an Action.
- I had one Servlet to do another thing defined in web.xml but that servlet made the portlet unbehaviour.
- I am able to change res' Headers and contentType, but if read in these forums that doing this doesn't work.
- my links are <a href="#" onclick="MyFuction();"> MyLink </a>

1
2
3function MyFunction(){
4
5//operations && submit
6
7}


Putting target="" on <a href="" ></a> is not adviseable because Action will be repeated in case you click on that link.

My action extends from Action.


I've seen that there is one example in which I can ask the portlet to get a file, but it uses the OutputStream again. (documentary library is the example I am talking about).

I'll keep looking for answers. Anyway, if you find out what I am doing wrong, I'll be greatfull if you tell me it.

Thank you
Ray Augé
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 8, 2008 7:03 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

May I ask which version of the portal are you using?
Agustin Campos
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 8, 2008 7:13 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

This is my LifeRay Version:

Starting Liferay Enterprise Portal 4.3.3 (Owen / Build 4303 / October 9, 2007)

I hope I won't have to change it.

If you could help me I would be very greatful. I've been working on it for weeks.

Thank you in advance
Ray Augé
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 8, 2008 7:55 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

I'm sorry to be the bearer of bad news, but doing this from withing portlets, in this version of Liferay, is not possible. The JSR-168 spec did not support it, and we hadn't implemented our own work around at that point.

So, you have to use a servlet call to handle it. IF you are using struts then it's not really that hard. Simply create a struts "Action" and delegate your request to that struts path (but you can't construct the URL to it using a portlet tag).

You're using the correct exmaple (doc lib), but also look closely at how we handle the creation of urls... you can't create portlet url which points to a struts path that is an servlet Action rather than a portlet Action and have it work.
Agustin Campos
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 8, 2008 8:57 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

I will have a look at how URLs are built.

I'll make you know I I manage to make this work.

Thank you a lot!!!
Agustin Campos
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 9, 2008 5:59 AM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

Hello all!!

I've managed to call an action using

1
2
3<html:link action="myPortlet/myPortletsAction"> CallToMyAction </html:link>



I could do the same as the document_library.

The case is that the Action I am calling works, res.OutputStream IS NOT null. So, I can work with it, fill it in with the file I want to get, etc, etc.

There is still one problem:

I am losing my portlet's contents and, instead of them, my fiiles content is written as clear text inside the portlet.

How did you say this issiue was solved?


Once more, thank you a lot!!
Ray Augé
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 9, 2008 8:07 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

Hmm, again I'm fairly certain that <html:link ... /> will result in a portletURL which won't work. You need to make sure the link hits your struts path directly, like:

1http://hostname/portal-context/struts-servlet/action/path?p1=v1&p2=v2...
Ivano Carrara
RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 10, 2008 9:24 AM
Answer

Ivano Carrara

Rank: Expert

Posts: 280

Join Date: July 2, 2005

Recent Posts

Hi to all! In fact it seams that it is impossible download a binary file from a portlet.

Of course I have to use a servlet deployed in the same war file of the portlet application, as we can read in the Jsr-168 specification.

But until today I can't do that... For example, in my very simple Jsr-186 portlet (not Strut, a very basic portlet):

In the doView method of my portlet:
1
2                // the resource object is my binary files got from Alfresco server that I wish download
3                PortletSession session = request.getPortletSession();
4    session.setAttribute("resource", resource, PortletSession.APPLICATION_SCOPE);
5
6    PortletRequestDispatcher namedDispatcher = portletContext.getNamedDispatcher("BinaryServlet");
7    namedDispatcher.include(request, response);


In the doGet of my BinaryServlet:
 1
 2                HttpSession session = request.getSession();
 3    WebdavResource resource = (WebdavResource) session.getAttribute("resource");   
 4       
 5    InputStream is = null;
 6                is = resource.getMethodData();
 7   
 8                response.setContentType (getMimeType(resource.getName()));
 9                response.setHeader ("Content-Disposition", "attachment; filename=\"" + resource.getName()+ "\";");
10                response.setHeader("Pragma", "cache");
11                response.setHeader("Cache-control", "cache");
12   
13                ServletOutputStream out = response.getOutputStream();   
14   
15                byte[] buffer = new byte[1024];
16
17                while (true) {
18                        int bytes = is.read(buffer);
19                        if (bytes <= 0) {
20                            break;
21                            }      
22                         out.write(buffer, 0, bytes);
23                        }
24                 i  s.close();
25                   out.flush();
26                   out.close();         
27                 }


The problem is that the binary content is always rendered by the portlet and you get the text content of the binary file inside the portlet windows..... Not exactly what we need.... !!!

Actually the only workaround is to render the portlet with a Jsp page, putting inside a link to the BinaryPortlet deployed externally from the war:

( Note: "${child[0]}" now is the name of the binary file to download and in the BinaryServlet I have to do the stuff to get the binary file form the Alfresco server)

1
2           (..... omissis......)    
3           <a href="/BinaryServlet?file=<c:out value="${child[0]}"/>" target="_new"><c:out value="${child[0]}"/></a>


Anyone can help on download the binary file directly from the doView method of the portlet?

Thank you in advance!

Ivano Carrara
Agustin Campos
[SOLVED] RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu
May 13, 2008 11:11 PM
Answer

Agustin Campos

Rank: Junior Member

Posts: 34

Join Date: February 18, 2008

Recent Posts

hi!!

I've managed to do it:

This is a little HowTo:

If I use <html:link action="MyStrutsAction/MyAction"> I could get the struts action path. To do it open the jsp page and look for that link in the source code.

Once you have found in the rendered <a href=""> you have to look for that action in iit.

Then, that will be your struts-servlet/action/path

All you have to do is change your <html:link ...> to <a href=""> with the URL your

http://hostname/"context"/"your_just_descovered_action_path"?param1=xxx&param2=yyy...

When you click that link your Action will be called.

People should know that "form" will be null.

Parameters are in req.gerParameter("param1");

That's all!!!
Ray Augé
Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi
May 27, 2008 11:58 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

May I ask which version of the portal are you using?
Ray Augé
Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi
May 27, 2008 11:58 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

> Hi to all! In fact it seams that it is impossible download a binary
> file from a portlet.


This is correct to a point.

As far as the JSR-168 spec was concerned, this wasn't a supported
feature. BUT, from certain versions of Liferay 4.4 we did support
delivering binary content from with portlet phases.

In later versions of 4.4 you can do this to send binary content from a
portlet render phase, BUT only if you use the
LiferayWindowState.EXCLUSIVE window state.

Here is an example:

 1    public void doView(RenderRequest req, RenderResponse res)
 2        throws IOException, PortletException {
 3
 4        boolean logo = ParamUtil.getBoolean(req, "logo");
 5
 6        if (logo && req.getWindowState().equals(LiferayWindowState.EXCLUSIVE))
 7{
 8            LiferayRenderResponse liferayRes = (LiferayRenderResponse)res;
 9
10            liferayRes.setContentType("image/png");
11            liferayRes.addHeader(
12                HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
13
14            OutputStream out = liferayRes.getPortletOutputStream();
15
16            InputStream in = getPortletContext().getResourceAsStream(
17                "/company_logo.png");
18
19            if (in == null) {
20                out.close();
21            }
22            else {
23                byte[] buffer = new byte[4096];
24                int len;
25
26                while ((len = in.read(buffer)) != -1) {
27                    out.write(buffer, 0, len);
28                }
29
30                out.flush();
31                in.close();
32                out.close();
33            }
34        }
35        else {
36            include(viewJSP, req, res);
37        }
38    }


and an url which triggers it:

1<img src="<portlet:renderURL
2            portletMode="view"
3            windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
4        <portlet:param name="logo" value="true" />
5    </portlet:renderURL>"
6    alt="Image returned by portlet." />
Rishi Dev Gupta
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel
December 8, 2008 10:35 PM
Answer

Rishi Dev Gupta

Rank: Expert

Posts: 255

Join Date: November 23, 2008

Recent Posts

Hi

I have tried the code example by you, and it worked very well, thanks.

But the issue is it works only in Firefox.

When try to open a pdf file in Internet Explorer version 6.0.2 it gives error -"There was an error opening opening this document. This file is already open or in use by another application".

I am using struts based portlet
I am using liferay 5.1.1, tomcat 6.0, jdk6.
I have checked all the input and ouput streams and they are closed properly and no other application is accessing the file also.

Do I need to have some other settings for Internet Explorer in portlet?
kameshwar singh
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel
January 7, 2009 10:18 AM
Answer

kameshwar singh

Rank: New Member

Posts: 14

Join Date: October 21, 2008

Recent Posts

Hi,

I am trying to export a .csv report using liferay 5.1 anf jsf, but unable to download it.

can anyone send me the code.
Vivek N Kapse
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel
March 20, 2009 3:09 PM
Answer

Vivek N Kapse

Rank: Junior Member

Posts: 42

Join Date: September 9, 2008

Recent Posts

Hi Ray,

I am having this issue of downloading an excel file from my struts portlet. I want to try what u have suggested but I couldn't get ur solution completely. I am using Liferay 5.1.1.
I have a struts reporting portelt. It generates a report and shows it in a jsp. I want to add 'export to excel file' option which wud download that same report in an excel.
Where does this doView method go? In a struts action?? Or does it go into a class? If it goes into a class how do i invoke this method using <portlet:renderURL>??? I am confused.
Looking at the <portlet:renderURL> tag i couldn't figure out how wud it be using this doView method. Will there any configuration change in web.xml file???
Can you please help me how shud i be designing this??
Minhchau Dang
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel
March 20, 2009 3:34 PM
Answer

Minhchau Dang

LIFERAY STAFF

Rank: Expert

Posts: 349

Join Date: October 22, 2007

Recent Posts

Scroll down to the "Liferay >= 5.0.0" section of Ray's blog entry.
Vivek N Kapse
RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel
March 24, 2009 3:04 PM
Answer

Vivek N Kapse

Rank: Junior Member

Posts: 42

Join Date: September 9, 2008

Recent Posts

The blog entry has the same stuff what this thread has. I still couldn't find out answers to my questions looking at the blog. Could anyone elaborate on this?