Oracle ERP 檔案上傳的範例


declare  
   v_access_id      NUMBER;
    l_server_url VARCHAR2(255);
    l_url VARCHAR2(255);
    l_gfm_id NUMBER;
    temp NUMBER;
    button_choice NUMBER;
    tempdata varchar2(32767);
    v_raw            raw(32767);
    v_int            number;
 
    v_len          number;
    v_i              number:=0;
    v_count       number:=0;
    v_char         varchar2(120);
   
    v_user            number;
    li_pos             INTEGER  := 0;
    li_old_pos       INTEGER  := 0;
    dummy           number;
   
   
    v_sou  varchar2(100);
    i number;
    j number;
    k number;
    v_tar varchar2(100);
    v_check varchar2(10);
   
    v_a  varchar2(100);
    v_b  varchar2(100);
    v_c  varchar2(100);
    v_d  varchar2(100);    
   
begin

    --Get Process ID
    v_access_id := fnd_gfm.authorize(null);
   
    --Get User Name
    v_user:=fnd_profile.value('USER_ID');    
   
    --取出APPS_WEB_AGENT的Web Server String
    fnd_profile.get('APPS_WEB_AGENT',l_server_url);

    --組成完整的URL,chr(38) --> &
    -- http://test.erp.com.tw:82266/pls/CLONE/fnd_file_upload.displayGFMform?access_id=849330512&l_server_url=http://test.erp.com.tw:82266/pls/CLONE
    l_url := RTRIM(l_server_url, '/') ||
        '/fnd_file_upload.displayGFMform?access_id=' ||
        to_char(v_access_id) || chr(38) || 'l_server_url=' ||
        l_server_url;

    IF (l_url IS NULL) THEN
        RAISE FORM_TRIGGER_FAILURE;
        RETURN;
    END IF;
       
    --開啟上傳網頁,上傳完必須自行關閉網頁。
    fnd_utilities.open_url(l_url);

    --設定提示視窗,用來決定是否完成上傳的動作
    fnd_message.set_name('FND','ATCHMT-FILE-UPLOAD-COMPLETE');
 
    --設定提示視窗的按鈕,有「YES」、「NO」,YES傳回1、NO傳回3
    button_choice := FND_MESSAGE.question(
                button1 => 'YES',
                button2 => null,
                button3 => 'NO',
                default_btn => 1,
                cancel_btn => 3,
                icon => 'question');

    --按下提示視窗的「YES」
    if button_choice = 1 then
               
        --回傳上傳檔案的ID
        l_gfm_id := fnd_gfm.get_file_id(_access_id);
       
               
        --檔案上傳頁面會將檔案上傳到 fn_lob 表格內的一個欄位,型態為 Blob。
        --檔案上傳結束。
       
       --取出檔案資料方法,
       /*
       以文字檔為例,上傳的範例資料為
         aa,bb,cc
         11,22,33
      */
           
      loop
            tempdata :='';
                       
           --chr(10)換行符號,li_pos為符合的字串長度,(li_old_pos +1)開始位置,1為符合chr(10)的第一筆資料
           --存在Blob欄位的資料是連續的raw字元
           --下列SQL會取出文字檔一行的資料,因為每一行是以斷行chr(10)做為分行
           --取出的資料格式是raw類型,不是一般的文字格式。
           select dbms_lob.instr(file_data, utl_raw.cast_to_raw(chr(10)), li_old_pos+1 , 1)
                  into li_pos
                  from fnd_lobs
                  where file_id = l_gfm_id;
                 
          exit when li_pos = 0 ;  -- no more data            
           
            begin
                --取出指定字串,(li_pos - li_old_pos-1) 字串長度,(li_old_pos +1) 開始的位置                
                  select utl_raw.cast_to_varchar2(dbms_lob.substr(file_data, (li_pos - li_old_pos-1), li_old_pos +1))
                  into  tempdata
                  from  fnd_lobs
                  where file_id = l_gfm_id;                    
                                   
                  ----字元集轉換-----------------------------------------------
                  --因為從Windows上傳的文字檔是ansi碼,但是資料庫是UTF8
                  select rtrim(convert(tempdata|| ' ','AL32UTF8','ZHT16MSWIN950')) into tempdata from dual;
                  --select rtrim(convert(tempdata|| ' ','AL32UTF8','ZHT16BIG5')) into tempdata from dual;
                  -----------------------------------------------------------------
                                             
           end;
         
            begin  
                select replace(tempdata,chr(13)||chr(10),chr(10)) into tempdata from dual;
            end;

            v_count := 0;
            v_char := null;
           select length(tempdata) into v_len from dual;
           for i in 1..v_len loop
               begin                            
                            --一次取一個字元,如果是",",則表示已完成一個字串,其用意與split分割函數同
                            --if nvl(substr(tempdata,i,1),',')=',' or ascii(substr(tempdata,i,1)) in('10','13') then    
                            if nvl(substr(tempdata,i,1),'n')=','  then
                                v_count:=v_count+1;                              
                                if v_count=1 then --字串aa 或 11
                                    v_a :=v_char;
                                    v_char:=null;
                                elsif v_count=2 then --字串bb 或  22
                                    v_char:=null;                                        
                                end if;
                            else
                                v_char:=v_char||substr(tempdata,i,1);      
                            end if;
                           
                            --每行的最後一個字串
                            if (i  = v_len-1) then
                                v_receive_no:=v_char;  --字串cc 或 33
                              v_char:=null;  
                            end if;
                           
               end;                  
           end loop;
         
           li_old_pos := li_pos;  -- pos of a file
         
    end loop;
   
end;

Windows 11安裝時跳過網路連線