Foren

Download pdf file using JSF portlet(Primefaces component)

Aryan sds, geändert vor 10 Jahren.

Download pdf file using JSF portlet(Primefaces component)

Junior Member Beiträge: 57 Beitrittsdatum: 24.04.12 Neueste Beiträge
Hi Liferay,

I am using Liferay 6.1.0 ga1 jsf-portlet.(Using primefaces command-button component)
I am trying to implement download pdf file functionality on button click in this way.

<p:commandbutton actionListener="#{backingBB.DownloadPDF(obj)}" update=":form" />


public void DownloadPDF(Employee obj) throws DocumentException, FileNotFoundException, IOException
	{


	PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
	HttpServletResponse response= com.liferay.portal.util.PortalUtil.getHttpServletResponse(portletResponse);
	//­We must get first our context
	FacesContext context = FacesContext.getCurrentInstance();

	//Then we have to get the Response where to write our file

	//Now we create some variables we will use for writting the file to the response
	String filePath = null;
	int read = 0;
	byte[] bytes = new byte[1024];

	String pathToTheFile="http:\\174.25.1.189:8080\html\pdf";
	//Be sure to retrieve the absolute path to the file with the required method
	filePath = pathToTheFile;
	System.out.println("PATH "+filePath);
	//Now set the content type for our response, be sure to use the best suitable content type depending on your file
	//the content type presented here is ok for, lets say, text files and others (like CSVs, PDFs)
	response.setContentType("application/pdf");

	//This is another important attribute for the header of the response
	//Here fileName, is a String with the name that you will suggest as a name to save as
	//I use the same name as it is stored in the file system of the server.

	String fileName="Aryan sds_07_007.pdf";
	response.setHeader("Content-Disposition", "attachment;filename=\"" +
	fileName + "\"");

	//Streams we will use to read, write the file bytes to our response
	FileInputStream fis = null;
	ServletOutputStream os = null;

	//First we load the file in our InputStream
	try {
	fis = new FileInputStream(new File(filePath,fileName));
	os = response.getOutputStream();

	} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}

	//While there are still bytes in the file, read them and write them to our OutputStream
/*	try {
	while((read = fis.read(bytes)) != -1){
	os.write(bytes,0,read);
	}
	os.flush();
	os.close();

	} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}*/

	//Clean resources

	//This option isn't quite necessary, It worked for me with or without it
	FacesContext.getCurrentInstance().responseComplete();

	//End of the method
	}


But I am getting IOException as

java.io.FileNotFoundException: http:\174.25.1.189:8080\html\pdf\Aryan sds_07_007.pdf (The filename, directory name, or volume label syntax is incorrect)


If I try to enter this url in browser window(from client machine) (above url is my server url & I am trying to download this file from any client machine)
it opens successfully.


So what I am missing in my path ?

Thanks,
Aryan sds
thumbnail
Neil Griffin, geändert vor 10 Jahren.

RE: Download pdf file using JSF portlet(Primefaces component)

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
I would recommend that you look at the source of the jsf2-export-pdf-portlet. It shows how to download a PDF using a JSF2 ResourceHandler. Also you can try the PrimeFaces p:fileDownload component.
thumbnail
Bernd Prager, geändert vor 10 Jahren.

RE: Download pdf file using JSF portlet(Primefaces component)

Junior Member Beiträge: 42 Beitrittsdatum: 20.12.12 Neueste Beiträge
Neil,

I remember you mentioning a discussion with Çağatay Çivici that the Primefaces download functionality is not portal compatible yet and was supposed to be fixed in the next version. The free version Primefaces 3.5 has not been updates since then AFAIK.

Am I wrong?

Thank you,
-- Bernd
thumbnail
Neil Griffin, geändert vor 10 Jahren.

RE: Download pdf file using JSF portlet(Primefaces component)

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Hi Bernd,

You are right -- FACES-1513 indicates that there is a problem with p:fileDownload. I just searched the PrimeFaces issue tracker and I don't see any corresponding issues logged there yet, so I doubt it is fixed in any of the PRO versions of PrimeFaces.

So at this time it's probably best to use a ResourceHandler.

Thanks,

Neil
Koffi AGHOSTO, geändert vor 9 Jahren.

RE: Download pdf file using JSF portlet(Primefaces component)

Junior Member Beiträge: 67 Beitrittsdatum: 27.05.14 Neueste Beiträge
Hello,

I am facing the same problem. I try <p:fileUpload> but I am still getting the same error : MimeType not recognized.
I use JSF2 and Liferay 6.2

Please, I want to know if somebody has found solution?

thank you in advanced
thumbnail
Kyle Joseph Stiemann, geändert vor 9 Jahren.

RE: Download pdf file using JSF portlet(Primefaces component)

Liferay Master Beiträge: 760 Beitrittsdatum: 14.01.13 Neueste Beiträge
Hi Koffi,
If you are trying to use p:fileUpload, you should create a new thread. This thread is about exporting/downloading data not uploading data.

- Kyle