ADF Faces File Uploading – It is really that simple!

One particularly type of interaction in web applications is typically a little bit tricky. Requiring additional analysis of the frameworks used. For me personally it was even the way to get started with Java technology (Jason Hunter’s Servlet Programming – that’s where for me it all began). I am talking about File Uploading. Functionality required in many applications, and frequently causing some headaches. I am working on an ADF project where we are considering offering an "Excel upload’ facility as method for getting data into the application. Doing some initial investigations, it struck me how extremely simple and straightforward the implementation of file upload with ADF Faces really is!

The steps:

set the usesUpload property of the af:form element to true

add an af:inputFile element to the form

bind the value attribute of this inputFile to a managed bean property of type UploadedFile (oracle.adf.view.faces.model.UploadedFile)

write code in the setter method for this property to actually do something with the uploaded file contents

And this is all you need to do! Let’s create a simple example that will serve as our stepping stone for uploading CSV and Excel files. We will create a simple application that can upload text files and display their contents and several file details:

Let’s first create a new JDeveloper Application with a new Project. In this project, create a new JSF page (FileUpload.jspx).

Use the af:form tag instead of the h:form and set the usesUpload attribute to true:

Now we can run the JSF page. We can press the browse button, select a file, press the Start Upload button and have the file uploaded to the application server. The FileProcessor Bean’s setUploadedFile method is invoked and the properties and contents are extracted from the UploadedFile object and set on the other bean properties.

26 Comments

Dear Lucas Thank you for your interesting article. Â Actually it was very helpful! Now I need a “progress bar” for my “file uploading”, showing the percent ofÂ completion, especially for the files with Â huge size. I don’t know how to do this part of the code. I’ll be thankful if you guide me on this matter. Regards. Â

hi, Â i managed to run the sample on the Jdev 11.1.2 🙂 here is what i did. . first i downloaded jfreereport-0.8.6.jar (for ExcelProcessor class) and jcommon-1.0.15.jar (for StackableException class) via jar search. . then replaced the <managed-bean-class>nl.amis.adffaces.excel.ExcelProcessor</managed-bean-class> with <managed-bean-class>org.jfree.report.modules.output.table.xls.ExcelProcessor</managed-bean-class> then ran the sample 🙂 hope this helps vosime

Hi Using your program where(location)will the file be placed on to server . In my case if I am running from local then the file should be uploaded to my local jdev directory structure. ButÂ I dont find the file uploaded.

Very useful!. I tested that in jDeveloper 10.1.3.3, and it works. Just a warning appeared in class “FileProcessor.java”, that was “DataInputStream readLine is deprecated”, but you can solve this using java class “BufferedReader” instead “DataInputStream”, like this:

hello….this application is running on jdeveloper11g..but it is not running on eclipse-tomcat envirnment..may be i have missed nay jar file.could u pls tell me to make this runnable there what all i have to do?

I am not sure exactly what you are doing, but it seems you are mixing up client and server with this approach. The ADF Middle Tier knows nothing about paths on the (browser) client. It cannot reach those paths, it is not allowed to. If in your case the webclient happens to be a machine the ADF Middle Tier happens to be able to get to, that is a coincidence that the architecture does not cater for – you still cannot get path information from the browser. Nor can you preset the value of the upload file item: that must be set by the user!

You do not get the path – after the file has been uploaded, it just has a name. For security reasons, the path on the browser-machine is not available to the server. (what would you need it for anyway?)

Thank you for your reply. However when i tried to make the same in Jdeveloper11g, the property of type oracle.adf.view.faces.model.UploadedFile gives an error(“not found”) . Also in the function MyProgressRangeModel, the method createValueBinding and GetValue have been deprecated in JDeveloper11g. Can you provide an alternate solution to that please?

Now I download ADFFacesUploadFile.zip. And I try it. At first time, three lines in jspx file have problem: xmlns:h=”http://java.sun.com/jsf/html” xmlns:f=”http://java.sun.com/jsf/core” xmlns:afh=”http://xmlns.oracle.com/adf/faces/html” xmlns:af=”http://xmlns.oracle.com/adf/faces” After installing jsp lib, it works.

But in faces-config.xml, has managed bean ExcelProcessor. And I could not find it’s java code.

I follow the instruction to crate an application in Jdeveloper. It works. However, when I trying to load other file, the file name, file type, and file size fields show the right information. The file contents files display the old file contents. I will appreciate if you can offer the code to refresh the file contents field and show the current file contents.

Yes I did. It really is this simple! The inputFile element is linked to the FileProcessor.uploadedFile property. This property is of type oracle.adf.view.faces.model.UploadedFile. When the Upload button is pressed the form is submitted and the uploadedFile property is set by ADF Faces. To check out the details, please download the zipfile you find under Resources.

Follow us on Twitter

meta

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 323 other subscribers

Email Address

About

AMIS is internationally recognized for its deep technological insight in Oracle technology. This knowledge is reflected in the presentations we deliver at international conferences such as Oracle OpenWorld, Hotsos and many user conferences around the world. Our AMIS Technology Blog, the most referred Oracle technology knowledge base outside the oracle.com domain. However you arrived here, we appreciate your interest in AMIS. Link to our Google+ Profile AMIS