我有一个这样的表格:
https://i.stack.imgur.com/LyTJW.png
我将文件路径保存在数据库中,但我也想将图像文件保存在服务器文件夹中。可以共享Oracle格式的查询或函数吗?
到目前为止,这是我的代码
DECLARE
a varchar2(20);--2018
v_directory varchar2(200);--mkdir D:\EMP\2018
filename varchar2(256); --D:\img.doc
v_last_slash_position NUMBER;--3
v_file_name VARCHAR2(30);--img.doc
v_extension VARCHAR2(4);--.doc
v_last_slash_position2 NUMBER;--7
path2 VARCHAR2(30);--D:\EMP\2018
v_file_name2 VARCHAR2(30);
BEGIN
SELECT TO_CHAR(sysdate,'YYYY')
INTO a
FROM dual;
v_directory:='mkdir D:\EMP\'||a;
filename := get_file_name(File_Filter=>'JPEG Image (*.JPG,*.JPEG,*.JPE,*.JFIF)|*.JPG|');
v_last_slash_position := INSTR(filename, '\', -1) + 1;
v_file_name := SUBSTR(filename, v_last_slash_position);
v_extension := SUBSTR(filename, -3);
v_last_slash_position2 := INSTR(v_directory, '\', 1) - 2;
path2 := SUBSTR(v_directory,7);
v_file_name2 := path2||'\'||v_file_name;
host(v_directory,NO_SCREEN);
MESSAGE(filename);
MESSAGE(v_file_name2);
-- HOST('cmd /d COPY '||filename||' '||v_file_name2,NO_SCREEN); This is not working
IF NOT Form_Success THEN
MESSAGE('Error -- File not copy.');
ELSE
MESSAGE('Success.');
END IF;
END;
预先感谢
[1]:
答案 0 :(得分:4)
在PL / SQL中,您可以使用以下过程:
CREATE OR REPLACE PROCEDURE SaveFile(
FileContent IN OUT NOCOPY BLOB
, FolderName IN VARCHAR2
, FileName IN VARCHAR2)
IS
BUFFER RAW(1024);
OFFSET PLS_INTEGER := 1;
FileLength PLS_INTEGER;
amount PLS_INTEGER := 1024;
fhandle UTL_FILE.FILE_TYPE;
BEGIN
FileLength := DBMS_LOB.GETLENGTH(FileContent);
fhandle := UTL_FILE.FOPEN(FolderName, FileName, 'wb');
LOOP
EXIT WHEN OFFSET > FileLength;
DBMS_LOB.READ(FileContent, amount, OFFSET, BUFFER);
UTL_FILE.PUT_RAW(fhandle, BUFFER, TRUE);
OFFSET := OFFSET + amount;
END LOOP;
UTL_FILE.FCLOSE (fhandle);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.IS_OPEN(fhandle) THEN
UTL_FILE.FCLOSE(fhandle);
END IF;
RAISE;
END SaveFile;