Tuesday, January 12, 2010

Upload file to Application server using OAFileUploadBean instead of Database

Sometimes, there can be requirement , where you need to upload a file on unix server, instead of database.You can use OAMessageFileUploadBean for this. Add the upload bean in your OAF UIX page. Add an additional submit button bean which will be responsible for file Upload.
On click on this button in process form request call the following method with parameters as described in CO. So, copy this method in CO and then call it in process form request on this submit button click like

uploadFileToServer(pageContext, "item3",
"/xx/app/sss/x");


Here is the method for upload:
/**
* @param pageContext is current pagecontext in CO
* @param fileuploadBeanId is item id of file upload bean
* @param server_dir_path is abslute path on unix server
* where file needs to be written.eg "/xx/xxx/xxx"
*/
public void uploadFileToServer(OAPageContext pageContext,
String fileuploadBeanId,
String server_dir_path)
{
DataObject fileUploadData =
(DataObject) pageContext.getNamedDataObject(fileuploadBeanId);
if(fileUploadData!=null)
{
String uFileName =
(String) fileUploadData.selectValue(null, "UPLOAD_FILE_NAME");
String contentType =
(String) fileUploadData.selectValue(null, "UPLOAD_FILE_MIME_TYPE");

File file = new File(server_dir_path, uFileName);
FileOutputStream output = null;
InputStream input = null;
try
{
output = new FileOutputStream(file);
BlobDomain uploadedByteStream =
(BlobDomain) fileUploadData.selectValue(null, uFileName);
input = uploadedByteStream.getInputStream();
for (int bytes = 0; bytes < uploadedByteStream.getLength(); bytes++)
{
output.write(input.read());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (input != null)
{
input.close();
}
if (output != null)
{
output.close();
output.flush();
}
}
catch (Exception ez)
{
ez.printStackTrace();
}
}

}
}

Please understand when you run a page from jdeveloper, you are running page on local oc4j server of jdeveloper and not the actual application server of Oracle Apps, hence if this code is run from jdeveloper then you need to give path of your local desktop folder and file will be uploaded in it and not the actual application server.
To test this code actually you should deploy your page onto Oracle apps application server and access the page from a responsibility by registering it as a function , because then it will use Oracle Apps Application Unix server and upload file there.

Happy coding ...!

39 comments:

Kamlesh Nikhade said...

Hi Mukul ,

I am very new to this and I have same requirnment for upload page.

Can you give me any docs or detail steps for this.

Thanks & Regards,
Kamlesh Nikhade.

Kamlesh Nikhade said...

It is giving me "Error(12,40): cannot access class oracle.apps.fnd.framework.webui.DataObject; file oracle\apps\fnd\framework\webui\DataObject.class not found"
plz reply on this..

Parag Narkhede said...

I am running page from JDEVELOPER my PC. I have used Browse field to take file and used its id in your uploadFileToServer procedure. Also I have given path as "/home/blink/". After clicking submit button which calls your procedure, no errors comes on page and file also not get transfer to my application server. My question is if I am running page from my PC how the code will come to know, directory path exist on which server?

Kamlesh Nikhade said...

Hi Mukul ,

All ERRORS RESOLVED NOW ..BUT WHEN I GIVE APPLICATION SERVER PATH FOR FILE UPLOAD..IT IS GIVING PATH NOT FOUND WHEN I RUN IT FROM JDEVELOPER.
PLZ REPLY.

Thanks ,
Kamlesh

Parag Narkhede said...

Kamlesh, Please register the page in oracle apps server and run from the front end. In that case it will understand the directory path.

Kamlesh Nikhade said...

Thanks Parag...

Kamlesh Nikhade said...

It is working now..Thanks..

Kamlesh Nikhade said...

Hi ,

I have two servers application and DB and now I am able to upload file on application server directory but my requirement is to upload the file on db server..
How can I achive this..please guide.

Regards,
Kamlesh

Mukul said...

Kamlesh and Parag, nature of error is same for both of you.

Please understand when you run a page from jdeveloper, you are running page on local oc4j server of jdeveloper and not the actual application server of Oracle Apps, hence if this code is run from jdeveloper then you need to give path of your local desktop folder and file will be uploaded in it and not the actual application server.
To test this code actually you should deploy your page onto Oracle apps application server and access the page from a responsibility by registering it as a function , because then it will use Oracle Apps Application Unix server and upload file there.

I hope this clarifies problem for both of you.
--Mukul

Mukul said...

Also this code will not upload file in database server. For uploading file in database server in a table u can directly use fileupload bean functionality.
--Mukul

phantom said...

Hi Mukul,
To implement the above requirement, whether the AM or VO to be extended?

Parag Narkhede said...

Hi Phantom, If I understand correctly, you need to extend CO if you are working on seeded page. If you are developing new page just put the code in CO and enjoy.. If you are using seeded page, decompile the CO of seeded page, and java code to add button on form and handle the event for this button in PFR. Hope this clarify

Mukul said...

I agree with Parag's reply? even i could not understand your question!Y u wanna do extension?
--Mukul

Andrea said...

Hi Mukul ,

Can you give me any docs or detailed steps for this?

Thanks & Regards,

Andrea

Kamlesh Nikhade said...

Hi All,

I did this & It's working fine..

Needs to Import following packages in your controller class and all is same as above:

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.cabo.ui.data.DataObject;
import java.io.FileOutputStream;
import java.io.InputStream;
import oracle.jbo.domain.BlobDomain;
import java.io.File;


Thanks & Regards,
Kamlesh Nikhade

Kamlesh Nikhade said...

Hi All,

To upload the file on DB Server , I used the NFS Mount utility of the unix.

Thanks & Regards,
Kamlesh Nikhade.

phantom said...

Hi Parag,
i need to add the upload file functionality in an existing page, thats the reason i asked for extension. Now i tried to create a page for uploading file, it gives error "No Current rows for the View", even if i initiate my view. Below is the code which i added in AM.
public void uploadFile()
{
UploadEOViewImpl vo = getUploadEOView1();

if (!vo.isPreparedForExecution())
{
vo.executeQuery();
}
Row row = vo.createRow();
vo.insertRow(row);

row.setNewRowState(Row.STATUS_INITIALIZED);
} . plz throw some light on this.

Parag Narkhede said...

Hi Phantom,
"Now i tried to create a page for uploading file".. Why are you creating new page? I am sure, you can satisfy your requirement by extending standard CO. Create a uploadfile bean in extended CO.. Correct me if I am wrong, Because I am also newbie in OAF.

phantom said...

Hi Parag,
Actually i am now just trying to create an individual page to upload a file to unix server just for training. In real time I have to extend a CO to implement it as you said.
P.S- i have already used upload file bean and iam calling the above method from my CO's PFR

public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
{
super.processFormRequest(pageContext, webBean);
String actionInScreen = pageContext.getParameter(EVENT_PARAM) ;
OAApplicationModule am = pageContext.getApplicationModule(webBean);

if (actionInScreen.equals("update"))
/* if (pageContext.getParameter("Attach")!= null ) /*&& pageContext.getParameter("upload"))*/
{
am.invokeMethod("uploadFile");

}

}

where update is my event name.

Parag Narkhede said...

Sorry for late reply. What problem you are facing?
1) Please check spelling of "update" or "Attach". Make sure that your IF condition is getting satisfied.
2) Check whether you have write permission on the directory where you want to transfer files.
3) what code you have written in am.invokeMethod("uploadFile");
4) Make sure that your page is registered in Oracle apps and you are testing this functionality from there.

This is what I have done. In PFR:
if (pageContext.getParameter("Upload_file") != null) {
uploadFileToServer(pageContext, "Browse_file1", "/usr/tmp");
}

and put code of uploadFileToServer in CO
Where Browse_file1 is upload bean and Upload_file is submit button.

Naresh said...

HI mukul,
Can please send me list of servers/containers like tomcat that support oracle applications on which our application is deployed.
regards
Naresh Kamra

Robert said...

Hi Mukul,

I have a generic upload page that saves the files in fnd_lobs.
What i experience is that when i use your code to save the file on the unix file system is that it is always 0 bytes.
I have the same issue when i put the extraction logic in a trigger on fnd_lobs.
What could i be doing wrong?

regards,
Robert

Vimal said...

Hi Mukesh, thanks for sharing the steps. I have couple of questions - 1. How do I give a relative name for the location where the file needs to be stored? for .e.g I need to give $XX_TOP instead of hardcoding the path. 2. How do i transfer the file in ascii mode.
Thanks.

SRIRAM said...

Hi Mukesh

I have implemented the above functionality. But the client requires the page in 3 languages. When i upload the file in english, it works fine. But when i upload the file in japanese, the content is transferred but the file name becomes junk characters. Any suggestions for this issue ?

Krzysiek Matuk said...

Hey,
do you have any idea how to obtain filePath from directory defined in the database:
CREATE OR REPLACE DIRECTORY
INTERFACE_INCOMING AS
'/usr/tmp';

Is there a way to use INTERFACE_INCOMING in Java as it can be used in utl_file.fopen(p_filepath, p_filename, p_mode);

regards
Krzysiek

Devi said...
This comment has been removed by the author.
Devi said...

Kamlesh Nikhade,

I have a similar requirement - upload file from OAF and move it to database server. I was able to move it to application server. Can you please let me know how did you use NFS mount utility for this?

Thanks in Advance!

Romina OJEDA said...

Hi Mukul,
I have a custom oaf page that has a FileUploadBean that is save in the database when the user click on an Apply button.
I noticed that if I raise an application (for example because of a business validation logic) before the commit action the file in the FileUploadBean is lost.
The field show the "View" link but when I click on it the image is empty and it neither have the correct filename and file extension, the page is trying to open a file called "View" without other name of extension and without content.
What I am missing? Is necessary some code step that I haven't included in the controller class?

neo said...

hi Mukul,

Using the file upload i am able to upload the file successfully, but the issue is that if the file name contains more than one continuous space, it is truncating the spaces into one single space. For example, if the file name is "New File Name.txt" then it is shown as "New File Name.txt". Kindly help on this.

Prologic Corporation said...

This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
healthcare, e commerce, programming, multi platform,inventory management, cloud-based solutions, it consulting, retail, manufacturing, CRM, technology means, digital supply chain management, Delivering high-quality service for your business applications,
Solutions for all Industries,
Getting your applications talking is the key to better business processes,
Rapid web services solutions for real business problems,
Web-based Corporate Document Management System,
Outsourcing Solution,
Financial and Operations Business Intelligence Solution,

Our address:
2002 Timberloch Place, Suite 200
The Woodlands, TX 77380
281-364-1799

prologic-corp

FAIZ ALAM said...

Hi,
Can u plz post the code to upload image into Database

Brajesh Kumar said...

Hi,
i have to upload multiple files from local PC directory to unix server. number of files not fixed. could you please help me.

Leslie Lim said...

I really like your ideas. I truly appreciate your effort in publishing this article. Keep it up and God bless.

Say
www.imarksweb.org

punam maskara said...

Hi Mukul,

I have the same requirement and I used this code but it doesn't support when we upload excel file.Can you please help me out on this.

Thanks,
Punam

punam maskara said...

Hi,

I tired all possible ways out but it is not working.Can anyone if not Mukul please help me on this.


Thanks,
Punam

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai

Raja said...

Hi,

How would I upload multiple files using OAF.

It could be single browser button to all files(attachment does the same but I want to use MessageFileUpload)

else browse button for each row in advanced table.

Thanks.

Raja said...
This comment has been removed by the author.
soumya dash said...

while uploading my file is getting generated at destination directory but the content of the file is missing. i.e. the file is created with 0 kb size. Please help on the same.