aspose file tools*
The moose likes JSF and the fly likes File input with input type=file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "File input with input type=file?" Watch "File input with input type=file?" New topic
Author

File input with input type=file?

Mark Dragan
Greenhorn

Joined: Sep 28, 2010
Posts: 18
Hi everyone,
I have question regarding uploading file, I'm using Jsf 2.0 and xhtml and I must provide user to create Pdf and in that Pdf to insert image, so I found the contol input and I'm
wondering when user selects image from the comp. the path is displayed in textbox of input control but how can I get that path in my Bean.
Thanks
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
You question is hard to understand. Can you rephrase your question?

If you are asking how can you upload a file with JSF then I recommend that you use some JSF tag library that supports it. For example I use MyFaces Tomahawk that has a easy to use inputFileUpload tag.

But if you must use the normal html input type then you can either implement the receiving side by yourself or use (yet again) some third party library that can handle uploaded files.

I must say that the first approach is a lot simpler...
Mark Dragan
Greenhorn

Joined: Sep 28, 2010
Posts: 18
Thanks for reply can you post me some link regarding how is that done with myFaces input file control.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
taglib api
http://myfaces.apache.org/tomahawk-project/tomahawk20/tlddoc/index.html

tag
http://myfaces.apache.org/tomahawk-project/tomahawk20/tlddoc/t/inputFileUpload.html

tomahawk
http://myfaces.apache.org/tomahawk/index.html

and (finally) instructions
http://wiki.apache.org/myfaces/Setup_For_File_Uploads


If you use the JSF 2.0 remember to use the latest version of tomahawk (1.1.10 or 1.1.11-snapshot)

EDIT: and while using the instructions modify the filter mapping so that it catches calls to jsf pages instead of "shtml".
Mark Dragan
Greenhorn

Joined: Sep 28, 2010
Posts: 18
Thanks, I have downloaded myFaces but I cannot see the way to include them into xhtml project,
when I set xmlns:t="http://myfaces.apache.org/tomahawk" I only get inputhtml component, but when I create another jsp page inside same project I can all components inside that page, any reason for that.
dev jha
Greenhorn

Joined: Oct 19, 2014
Posts: 2
Here i m creating pdf by using this method but pdf is not coming fully means half of left side is coming or fully table is not coming

and one more requirement is one popup should be generate and it should download with new popup and by default it should download in system download folder with download popup


public void projectGenerateReport() {

String outputFile = "C:\\Users\\djha\\Desktop\\firstdoc.pdf";

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext
.getRequest();
HttpSession session = (HttpSession) externalContext.getSession(true);

String servername = externalContext.getRequestServerName();
String port = String.valueOf(externalContext.getRequestServerPort());
String appname = externalContext.getRequestContextPath();
String protocol = externalContext.getRequestScheme();

String url = protocol + "://" + servername + ":" + port + appname + "/myeln/printProjectDetails.xhtml;jsessionid="+session.getId().toString();

HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
response.reset();


response.setContentType("application/pdf");


try {

OutputStream os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);

os.close();
} catch (com.lowagie.text.DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



and xhtml code for this

<p:menubar style="padding:5px; width:99%;">
<p:menuitem value="Generate Report" style="width:95%; font-weight:bold; color:#0431B4;" actionListener="#{projectManagedBean.projectGenerateReport()}"></p:menuitem>
<p:menuitem></p:menuitem>



Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

JSF is good for a lot of things. But those things are related to HTML forms.

I recommend that you do NOT use JSF to generate the PDF document. Use an ordinary servlet. It's simpler.

Also, it's very bad practice to meddle with jsessionid in your application code. Let the server handle that. For one thing, the server can - and does - change the jsessionid without notifying the application.


Customer surveys are for companies who didn't pay proper attention to begin with.
dev jha
Greenhorn

Joined: Oct 19, 2014
Posts: 2
give some proper solution
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

http://www.coderanch.com/how-to/java/NotACodeMill
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2597
    
    9

Doesn't MyFaces has its own file upload tag? Use that instead of using tomahawk or another tag "just for upload".

In my JSF project back then, I used primefaces. At first I also used tomahawk for upload but it turned out not very good when everything else used primefaces (not just in the backend logic yet the front end is hardly a different).

For primefaces, the backend logic will use the org.primefaces.model.UploadedFile class to indicate the the file is "an upload file".

A friendly reminder, when parsing the file path, beware of the difference in browser. Eg IE gives the full path like C:\..... where Firefox gives the last part only when you do something like UploadedFile#getFileName.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File input with input type=file?