Fórum

getting files (pdf, excel, doc, gif, jpg, tif..) + outputstr

Agustin Campos, modificado 15 Anos atrás.

getting files (pdf, excel, doc, gif, jpg, tif..) + outputstr

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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.
thumbnail
Eric Devlin, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Regular Member Postagens: 154 Data de Entrada: 19/02/08 Postagens Recentes
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, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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?
thumbnail
Eric Devlin, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Regular Member Postagens: 154 Data de Entrada: 19/02/08 Postagens Recentes
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, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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>



function MyFunction(){

//operations &amp;&amp; submit

}



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
thumbnail
Ray Augé, modificado 15 Anos atrás.

Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi

Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
May I ask which version of the portal are you using?
thumbnail
Ray Augé, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
May I ask which version of the portal are you using?
Agustin Campos, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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
thumbnail
Ray Augé, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
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, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
Hello all!!

I've managed to call an action using



<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!!
thumbnail
Ray Augé, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
Hmm, again I'm fairly certain that [tt]<html:link ... />[/tt] will result in a portletURL which won't work. You need to make sure the link hits your struts path directly, like:

http://hostname/portal-context/struts-servlet/action/path?p1=v1&amp;p2=v2...
thumbnail
Ivano Carrara, modificado 15 Anos atrás.

RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

Expert Postagens: 345 Data de Entrada: 03/07/05 Postagens Recentes
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:

                // the resource object is my binary files got from Alfresco server that I wish download
                PortletSession session = request.getPortletSession();
	session.setAttribute("resource", resource, PortletSession.APPLICATION_SCOPE);

	PortletRequestDispatcher namedDispatcher = portletContext.getNamedDispatcher("BinaryServlet");
	namedDispatcher.include(request, response);


In the doGet of my BinaryServlet:

                HttpSession session = request.getSession();
	WebdavResource resource = (WebdavResource) session.getAttribute("resource");	
		
	InputStream is = null;
                is = resource.getMethodData();
	
                response.setContentType (getMimeType(resource.getName()));
                response.setHeader ("Content-Disposition", "attachment; filename=\"" + resource.getName()+ "\";");
                response.setHeader("Pragma", "cache");
                response.setHeader("Cache-control", "cache");
    
                ServletOutputStream out = response.getOutputStream();    
    
                byte[] buffer = new byte[1024];

                while (true) {
                        int bytes = is.read(buffer);
                        if (bytes &lt;= 0) {
                            break;
                            }       
                         out.write(buffer, 0, bytes);
                        }
                 i  s.close();
                   out.flush();
                   out.close();         
                 }


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)


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


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

Thank you in advance!

Ivano Carrara
thumbnail
Ray Augé, modificado 15 Anos atrás.

Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel, doc, gi

Liferay Legend Postagens: 1197 Data de Entrada: 08/02/05 Postagens Recentes
> 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:

	public void doView(RenderRequest req, RenderResponse res)
		throws IOException, PortletException {

		boolean logo = ParamUtil.getBoolean(req, "logo");

		if (logo &amp;&amp; req.getWindowState().equals(LiferayWindowState.EXCLUSIVE))
{
			LiferayRenderResponse liferayRes = (LiferayRenderResponse)res;

			liferayRes.setContentType("image/png");
			liferayRes.addHeader(
				HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");

			OutputStream out = liferayRes.getPortletOutputStream();

			InputStream in = getPortletContext().getResourceAsStream(
				"/company_logo.png");

			if (in == null) {
				out.close();
			}
			else {
				byte[] buffer = new byte[4096];
				int len;

				while ((len = in.read(buffer)) != -1) {
					out.write(buffer, 0, len);
				}

				out.flush();
				in.close();
				out.close();
			}
		}
		else {
			include(viewJSP, req, res);
		}
	}


and an url which triggers it:

<img src="<portlet:renderURL 
			portletMode=" view" windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
		<portlet:param name="logo" value="true" />
	" 
	alt="Image returned by portlet." /&gt;
thumbnail
bluejjack D G, modificado 15 Anos atrás.

RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel

Expert Postagens: 255 Data de Entrada: 23/11/08 Postagens Recentes
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, modificado 15 Anos atrás.

RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel

New Member Postagens: 14 Data de Entrada: 22/10/08 Postagens Recentes
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, modificado 15 Anos atrás.

RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel

Junior Member Postagens: 42 Data de Entrada: 09/09/08 Postagens Recentes
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??
thumbnail
Minhchau Dang, modificado 15 Anos atrás.

RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel

Liferay Master Postagens: 598 Data de Entrada: 22/10/07 Postagens Recentes
Scroll down to the "Liferay >= 5.0.0" section of Ray's blog entry.
Vivek N Kapse, modificado 15 Anos atrás.

RE: Re: [Liferay Forums][3. Development] RE: getting files (pdf, excel

Junior Member Postagens: 42 Data de Entrada: 09/09/08 Postagens Recentes
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?
Agustin Campos, modificado 15 Anos atrás.

[SOLVED] RE: getting files (pdf, excel, doc, gif, jpg, tif..) + outpu

New Member Postagens: 23 Data de Entrada: 18/02/08 Postagens Recentes
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!!!