【问题标题】:How do I copy a BLOB image to the filesystem如何将 BLOB 映像复制到文件系统
【发布时间】:2017-12-27 12:00:02
【问题描述】:

我在这里得到了建议

https://softwareengineering.stackexchange.com/questions/362992/all-images-on-a-single-table-is-a-good-idea/363007#363007

将图像存储在文件系统中,而不是作为表中的 BLOB。

如何将 BLOB 复制到文件中,并获取其路径(供 APEX 应用程序访问)?

我正在考虑制作一个触发器,将 BLOB 图像保存到文件系统,并将路径保存到 varchar 列。

表格的结构类似于

CREATE TABLE "ALLIMAGES_TBL" 
   (    "ID" NUMBER NOT NULL ENABLE, 
        "NAME" NVARCHAR2(400), 
        "FILENAME" VARCHAR2(350 BYTE), 
        "MIME_TYPE" VARCHAR2(255 BYTE), 
        "SIZE" NUMBER, 
        "CHARSET" VARCHAR2(128 BYTE), 
        "LAST_UPDATE_DATE" DATE, 
        "BinaryBLOB" BLOB, 
        "PATH" varchar(260), 

【问题讨论】:

    标签: sql oracle11g oracle-apex


    【解决方案1】:

    像往常一样,两个选项都有 pro 和 contra:将图像保存在数据库中,或者将它们保存在文件系统中。在我看来,如果只有几个(好吧,让我们以大约 100 秒来衡量),我宁愿把它们放在一张桌子上。处理它们更容易。因为,一旦它们进入文件系统,您就必须使用更多 (HTML) 代码在 Apex 中获取和显示它们。

    无论如何,我是这样做的(在以前的 Apex 版本之一中,我相信它是 4.0):使用“文件浏览”项我将图像临时存储到表中(它是 WWW_FLOW_FILES;你宁愿使用你的现在拥有自己的表),然后 - 在通过按下按钮触发的过程中 - 我将它们移动到一个目录中。

    流程代码:

    declare
      -- l_dest_dir is name of the directory (Oracle object) which points
      -- to a filesystem directory
      l_dest_dir varchar2(30) := pkg_slike.f_dir_slike_upload_dp(:P0_DP);
    begin
      for cur_r in (select id, filename 
                    from wwv_flow_files
                    where upper(filename) like 'TS%.JP%G' 
                   )
      loop
        -- put a picture into L_DEST_DIR
        pkg_slike.p_write_blob_to_file(cur_r.id, l_dest_dir);
    
        delete from wwv_flow_files
          where id = cur_r.id;
      end loop;
    end;
    

    程序代码:

       PROCEDURE p_write_blob_to_file (p_file_id IN NUMBER, p_dir IN VARCHAR2)
       IS
          /* 19.04.2012. Taken from Eddie Awad's Blog
                         http://awads.net/wp/2011/09/20/create-an-application-to-upload-files-using-oracle-apex-in-less-than-10-minutes-video/
          */
          l_blob            BLOB;
          l_blob_length     INTEGER;
          l_out_file        UTL_FILE.file_type;
          l_buffer          RAW (32767);
          l_chunk_size      BINARY_INTEGER := 32767;
          l_blob_position   INTEGER := 1;
          l_file_name       pkg_general.subt_ime_slike;
       BEGIN
          -- Retrieve the BLOB for reading
          SELECT blob_content, filename
            INTO l_blob, l_file_name
            FROM wwv_flow_files
           WHERE id = p_file_id;
    
          -- Retrieve the SIZE of the BLOB
          l_blob_length := DBMS_LOB.getlength (l_blob);
    
          -- Open a handle to the location where you are going to write the BLOB
          -- to file.
          -- NOTE: The 'wb' parameter means "write in byte mode" and is only
          --       available in the UTL_FILE package with Oracle 10g or later
          l_out_file :=
             UTL_FILE.fopen (p_dir,
                             l_file_name,
                             'wb' -- important. If ony w then extra carriage return/line brake
                                 ,
                             l_chunk_size);
    
          -- Write the BLOB to file in chunks
          WHILE l_blob_position <= l_blob_length
          LOOP
             IF l_blob_position + l_chunk_size - 1 > l_blob_length
             THEN
                l_chunk_size := l_blob_length - l_blob_position + 1;
             END IF;
    
             DBMS_LOB.read (l_blob,
                            l_chunk_size,
                            l_blob_position,
                            l_buffer);
             UTL_FILE.put_raw (l_out_file, l_buffer, TRUE);
             l_blob_position := l_blob_position + l_chunk_size;
          END LOOP;
    
          -- Close the file handle
          UTL_FILE.fclose (l_out_file);
       END p_write_blob_to_file;
    

    希望您能够根据自己的情况进行调整。

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多