4 Replies Latest reply on Apr 4, 2017 9:28 PM by viman

    Problem of downloading a file in Apex 5.1

    viman

      I need to implement downloading a file from the page when I go to it.
      On the page, the process is implemented in "Pre-Rendering" - "Before Header", but the recommended technique for implementing the file download does not work. The contents of the file are displayed on the page as an inline instead of downloading the file. I read about the problem of HTTP headers (mime) in version 5 of Apex and applied the recommendations in the code. But this does not work:
         
      Sys.htp.flush;
         
      Sys.htp.init;
         
      Sys.owa_util.mime_header ('application/octet-stream', FALSE, 'UTF-8');
         
      Sys.htp.p ('Content-length:' || sys.dbms_lob.getlength (v_file_body));
         
      Sys.htp.p ('Content-Disposition: attachment; filename = "FILENAME.TXT"');
         
      Sys.owa_util.http_header_close;
         
      Wpg_docload.download_file (v_file_body);

      Where "v_file_body" is a blob. In Apex 4.2, this technique worked fine and without the recommended "sys.htp.flush; sys.htp.init" for version 5. What am I doing wrong?
      P.S: Sorry for the google translation ...

        • 1. Re: Problem of downloading a file in Apex 5.1
          Pavel_p

          Hi,

          your code looks OK, so maybe there is just your browser setting. What browser do you use?

          Also try to change your mime header from 'application/octet-stream' to 'text/plain' and page processing should be stopped after the download, so add

          APEX_APPLICATION.STOP_APEX_ENGINE;

          after the download_file statement.

          Regards,

          Pavel

          • 2. Re: Problem of downloading a file in Apex 5.1
            viman

            I'm using the latest versions of Google Chrom and Mozilla Firefox.
            I did not indicate this in the message, but of course I added the

            APEX_APPLICATION.STOP_APEX_ENGINE;

            instruction to the code.
            Trying to change 'application/octet-stream' to 'text/plain' does not bring success. The page is given to the browser as "text/html", which raises the problem of inline displaying the contents of the file on the page.
            What settings for browsers can I change? Browser settings by default. Files from Internet sites are downloaded normally.

            I forgot to add that Tomcat 8 + ords 3.0.9 is used when working with apex.

            • 3. Re: Problem of downloading a file in Apex 5.1
              Pavel_p

              Unfortunately I have no idea what's wrong with your setup... I modified a little bit a packaged application "Sample Upload and Download" to demonstrate that it works. Please, login to my workspace on https://apex.oracle.com/pls/apex/

              workspace: testing

              user: test

              pwd: test

              application: Application 99699 - Sample File Upload and Download

              and click the Files menu.There is a slightly modified Interactive report (added the last column - redirect to the page 1000 that downloads the file in a before header process). The code that should download any file as an attachment:

              declare
                  l_file_blob blob;
                  l_file_name eba_demo_files.filename%type;
                  l_file_mimetype eba_demo_files.file_mimetype%type;
              begin
                  select file_blob , file_mimetype , filename into l_file_blob , l_file_mimetype , l_file_name from eba_demo_files where id = :p1000_file_id;
                 
                  owa_util.mime_header( l_file_mimetype , false );
                  htp.p('Content-Disposition: attachment; filename="' || l_file_name ||'"');
                  htp.p('Content-length: ' || dbms_lob.getlength( l_file_blob ));
                  owa_util.http_header_close;
                  wpg_docload.download_file( l_file_blob );
                  -- Stop page processing
                  apex_application.stop_apex_engine ;
              end;
              

              If you still had problems downloading your file as an attachment, please, idemonstrate your problem on apex.oracle.com (if you don't have your account yet, create a new one, it's highly useful in situations like this).

              • 4. Re: Problem of downloading a file in Apex 5.1
                viman

                Pavel_p, thanks for the response and the attempt to help. I rechecked the code, but that's right. As a result, I decided that I created a new page, I used the same code and it works normally. What is the difference with the previous page, I could not understand ...
                Excuse for troubling.