Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File input with input type=file?

 
Mark Dragan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply can you post me some link regarding how is that done with myFaces input file control.
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dev jha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
give some proper solution
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/how-to/java/NotACodeMill
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic