Search code examples
oracle-apexoracle-apex-5oracle-apex-5.1

How to give link for users to Download the Csv Template in Apex


I have an requirement to give link to users in Apex Application to download the csv template.

I have created a link and created an process code as below for the users to download the csv file, But its showing some json error,

begin
-- Set the MIME type
owa_util.mime_header( 'application/octet', FALSE );
-- Set the name of the file
htp.p('Content-Disposition: attachment; filename="test.csv"');
-- Close the HTTP Header
owa_util.http_header_close;
-- Loop through all rows in EMP
for x in (select WORKSPACE,GROUPNAME,MEMBERS from CSV_NON_DYNAMIC_TEMPLATE where FILENAME='test.csv')
loop
 -- Print out a portion of a row,
 -- separated by commas and ended by a CR
 htp.prn(x.WORKSPACE ||','|| x.GROUPNAME ||','||x.MEMBERS|| chr(13));
end loop;
-- Send an error code so that the
-- rest of the HTML does not render
htmldb_application.g_unrecoverable_error := true;
end;

Even i code the process code to on load process, then its working the excel is creating but other than that no buttons or process code not working on the same page.

thanks


Solution

  • First, saying you're getting "some json error" is not helpful. It helps to put the exact error message you're getting in your question.

    Second, you probably have this process running at the Process step. Apex is submitting the page via Ajax, your process runs, returns an error to the page and you're getting your message.

    1. Change your process to run at the Before Header point and change it so it has a server side condition of "Request = Value" and make the value "DOWNLOAD".
    2. Change your button's action to Redirect to Page In This Application, and set the link to your page number with a request of "DOWNLOAD" (that's under the advanced section).