0

So, I'm currently trying to download an xml file stored in an Oracle table with a link I created for an Oracle Apex page. Here's the setup:

The table can contain 2 different files for xml stored as blob. With that I'm trying to have a report on Oracle Apex with a link to download these blobs, one for each type of file for every line of data.

The query I have to set up the links is the following:

select max(case when a.filename like 'TYPE1%' then dbms_lob.getlength(a.upload_file) else null end) as upload_type1,
       max(case when a.filename like 'TYPE2%' then dbms_lob.getlength(a.upload_file) else null end) as upload_type2
from blob_table a;

Now the page report should have 2 columns with a different link to each one, set to the size of each one of these. The links themselves are set to the same page using "Type1" and "Type2" respectively as a request, which are meant to trigger the following PLSQL function to download the files:

declare

  l_blob_content blob_table.upload_file%TYPE;
  l_mime_type    varchar2(20);
  l_id           number;
  l_filename     varchar2(255);

begin

    select id
    into l_id
    from blob_table
    where file_id = :FILE_ID
    and filename like 'TYPE1%';

    select upload_file
           ,'text/xml',
           filename
    into   l_blob_content,
           l_mime_type,
           l_filename
    from   blob_table
    where  id = l_id;

    sys.HTP.init;
    sys.OWA_UTIL.mime_header(l_mime_type, FALSE, 'UTF-8');
    sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(l_blob_content));
    sys.HTP.p('Content-Disposition: filename="' || l_filename || '"');
    sys.OWA_UTIL.http_header_close;

    sys.WPG_DOCLOAD.download_file(l_blob_content);
    apex_application.stop_apex_engine;
    exception when  apex_application.e_stop_apex_engine then
        null;
    when others then
        HTP.p('Something wrong');
end;

So far from what I've seen when searching this functionality, it all seems in place. The message I get however when clicking the link and trying to download it is this:

enter image description here

At first I thought I had something wrong and also tested the mime_type as "application/xml", and the file is shown in the page in an HTML format. Any ideas why the "text/xml" is giving an encoding error? Does it have to do with the 3rd input value for the mime_type?

2 Answers 2

2

'application/xml' is likely the correct mime_type. Your Content-Disposition is wrong. Are you trying to download the file or show it in the browser? If you want to show it in the browser then use the following:

sys.HTP.p('Content-Disposition: inline');

If you want to download the file then use the following:

sys.HTP.p('Content-Disposition: attachment; filename="' || l_filename || '"');

Also, I'm not sure when it was added, but in APEX v20.1 there's a Sample File Upload and Download app in the App Gallery that you can use to see how this can be done declaratively.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to download it, yes. The only reason I attempted the "application/xml" was to see if the codes I found for the mime types were accurate. I'll try both approaches to it, thanks! Quick edit: It worked! I attempted to follow an example that didn't contain the "attachment" part and it worked after adding it, thanks Dan!
Glad to hear it! :)
0

A couple things do not seem right.

  1. Where is that error message "Encoding error" coming from? Is that from an editor? It doesn't seem like an Oracle error message.
  2. Is your XML document truly stored as a binary LOB? If so, are you positive that the data encoding is truly utf-8? How have you verified this?

1 Comment

1. It simply appears on the browser after clicking the link that is supposed to download the blob file. I'm not sure where it comes from. 2. It's stored in a BLOB column in the database, though I'm not 100% sure on the encoding. I'll verify that as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.