在 Oracle Apex 中将图像插入到表中
Posted
技术标签:
【中文标题】在 Oracle Apex 中将图像插入到表中【英文标题】:Inserting an image into a table in Oracle Apex 【发布时间】:2015-12-23 20:58:42 【问题描述】:我根据下表创建了一个表单:
CREATE TABLE "POSTS"
( "POST_ID" NUMBER(15,0) NOT NULL ENABLE,
"USER_ID" VARCHAR2(30) NOT NULL ENABLE,
"TITLE" VARCHAR2(255) NOT NULL ENABLE,
"TEXT" CLOB,
"RESPONSE_TO" NUMBER(15,0),
"FLAGGED" CHAR(1),
"MIMETYPE" VARCHAR2(50),
"LAST_UPDATE" DATE,
"THUMB" BLOB,
"IMAGE" "ORDSYS"."ORDIMAGE" ,
"FILENAME" VARCHAR2(255),
CONSTRAINT "POSTS_PK" PRIMARY KEY ("POST_ID")
USING INDEX ENABLE
)
/
ALTER TABLE "POSTS" ADD CONSTRAINT "POSTS_CON" FOREIGN KEY ("RESPONSE_TO")
REFERENCES "POSTS" ("POST_ID") ENABLE
/
CREATE INDEX "POSTS_TEXT_INDEX" ON "POSTS" ("TEXT")
INDEXTYPE IS "CTXSYS"."CONTEXT"
/
CREATE INDEX "POSTS_TITLE_INDEX" ON "POSTS" ("TITLE")
INDEXTYPE IS "CTXSYS"."CONTEXT"
/
CREATE OR REPLACE EDITIONABLE TRIGGER "BI_POSTS"
before insert on "POSTS"
for each row
begin
if :NEW."POST_ID" is null then
select "POSTS_SEQ".nextval into :NEW."POST_ID" from sys.dual;
end if;
end;
/
ALTER TRIGGER "BI_POSTS" ENABLE
/
CREATE OR REPLACE EDITIONABLE TRIGGER "NEW_POST"
BEFORE INSERT
ON posts
FOR EACH ROW
BEGIN
--l_image := ORDSYS.ORDImage.Init();
--ORDSYS.ORDImage.process(:new.image, 'maxscale=200 200');
:new.user_id := v(':APP_USER');
--process_post_image(:new.post_id);
END;
/
ALTER TRIGGER "NEW_POST" ENABLE
/
标题、文本和文件名字段在表单中是可编辑的。
要上传文件,在自定义过程中使用以下代码代替自动生成的插入代码:
DECLARE
l_upload_size INTEGER;
l_upload_blob BLOB;
l_image_id NUMBER;
l_image ORDSYS.ORDImage;
BEGIN
--
-- Get the BLOB of the new image from the APEX_APPLICATION_TEMP_FILES (synonym for WWV_FLOW_TEMP_FILES)
-- APEX 5.0 change from APEX_APPLICATION_FILES which has been deprecated
-- APEX_APPLICATION_TEMP_FILES has fewer columns and is missing doc_size
--
SELECT
blob_content
INTO
l_upload_blob
FROM
apex_application_temp_files
WHERE
name = :P16_FILENAME;
--
-- Insert a new row into the table, initialising the image and
-- returning the newly allocated image_id for later use
--
INSERT
INTO
posts
(
post_id,
title,
text,
filename,
image
)
VALUES
(
posts_seq.nextval,
:P16_TITLE,
:P16_TEXT,
:P16_FILENAME,
ORDSYS.ORDImage()
)
RETURNING
post_id, image
INTO
l_image_id, l_image;
-- find the size of BLOB (get doc_size)
l_upload_size := dbms_lob.getlength(l_upload_blob);
-- copy the blob into the ORDImage BLOB container
DBMS_LOB.COPY( l_image.SOURCE.localData, l_upload_blob, l_upload_size );
-- set the image properties
l_image.setProperties();
UPDATE
posts
SET
image = l_image -- original ORDImage image
WHERE
post_id = l_image_id;
END;
但是会产生以下错误:
Invalid action CREATE on this object. (D)
【问题讨论】:
【参考方案1】:如果你移动 blob,我可以使用
成功地将 blob 转换为 ordImageUPDATE images
SET ord_image = ORDSYS.ORDImage(image)
WHERE ...
这里的触发器需要去掉冒号
:new.user_id := v('APP_USER');
我经常用
COALESCE(apex_application.g_user, sys_context('userenv','session_user'))
【讨论】:
我还可以使用更新查询从 BLOB 设置 ORDImage,但之后我无法使用 ORDImage 的实际功能,如select post_id, t.image.getfileformat(), t.image.getcompressionformat(), t.image.getcontentformat(), t.image.getcontentlength() from posts t;
如果您或 @HedgepigMatt 有任何想法,请帮助我,我如果可能的话,我正在寻找一种通过 Apex 表单页面直接将图像存储到 ORDImage 的方法。另外我想知道这个 BLOB 到 ORDImage 的过程会影响上传媒体的质量吗?
ORDImages 只是存储更深一层的 blob。当您尝试该功能时会发生什么?以上是关于在 Oracle Apex 中将图像插入到表中的主要内容,如果未能解决你的问题,请参考以下文章