使用 Coldfusion 从 Postgres 存储和检索图像

发布于 2024-10-04 19:26:30 字数 966 浏览 3 评论 0原文

我正在将数据库从 mssql express 2005 迁移到 postgresql 9.0。 在 mssql 中,列是图像类型,在 postgresql 中,我使用 bytea 类型。

<cffile 
       action="readbinary" 
       file="#ExpandPath('./uploads/')##theLogo.SERVERFILE#" 
       variable="myLogo">

<cfquery 
        name="saveLogo" 
        datasource="#SESSION.DSN#">
        UPDATE bright.group SET LOGO =  <cfqueryparam  
                                                       cfsqltype="cf_sql_blob"                    
                                                       value="#myLogo#">        
</cfquery>

当我保存时,上面的代码片段似乎有效,但是当我尝试使用下面的代码片段显示图像时,我什么也没得到。

<cfquery 
       datasource="#SESSION.dsn#" 
       name="image">
        SELECT LOGO
        FROM bright.group
        WHERE groupid=#URL.groupid#
</cfquery>
<cfcontent variable="#image.LOGO#" type="image/png">

这适用于 mssql,但不适用于 postgres。

任何帮助/指导将不胜感激。 谢谢

I am in the process of migrating a database from mssql express 2005 to postgresql 9.0.
In mssql the column is an image type and in postgresql i'm using the bytea type.

<cffile 
       action="readbinary" 
       file="#ExpandPath('./uploads/')##theLogo.SERVERFILE#" 
       variable="myLogo">

<cfquery 
        name="saveLogo" 
        datasource="#SESSION.DSN#">
        UPDATE bright.group SET LOGO =  <cfqueryparam  
                                                       cfsqltype="cf_sql_blob"                    
                                                       value="#myLogo#">        
</cfquery>

The snippet above seems to work when I do a save but when I try to display the image with the snippet below I get nothing.

<cfquery 
       datasource="#SESSION.dsn#" 
       name="image">
        SELECT LOGO
        FROM bright.group
        WHERE groupid=#URL.groupid#
</cfquery>
<cfcontent variable="#image.LOGO#" type="image/png">

This works on mssql but not on postgres.

Any help/direction would be greatly appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-10-11 19:26:30

您可以进行适当的重构并放弃将图像保存在数据库中吗?

使用二进制文件的文件系统和数据库来保存这些资源的路径。它将加快您的应用程序并使未来的迁移变得更加容易。

Could you do a proper refactoring and abandon saving images in the database?

Use the file system for binary files and the database to keep the paths to these resources. It's going to speed up your application and make any future migration much easier.

慢慢从新开始 2024-10-11 19:26:30

不知道太多来解释真正的机制,但这就是我发现的。
图像的存储方式不同。我在十六进制查看器中打开返回的图像。 postgresql 文本从第二个字节到末尾与 mssql 十六进制值匹配。

我尝试了一切方法来转换 postgresql 输出,但没有成功。第一个字节不同并没有帮助。
最后我将数据类型从 bytea 更改为 text 并保存编码的二进制文件。

<cffile action="readbinary" file="#ExpandPath('./logo.png')#" variable="myLogo">
<cfquery name="saveLogo" datasource="#session.dsn#">
        UPDATE bright.group SET LOGO = 
        (<cfqueryparam value="#BinaryEncode(myLogo,'hex')#" cfsqltype='cf_sql_longvarchar'> )
    </cfquery>

<cfquery datasource="#session.dsn#" name="qryGetLogo">
        SELECT logo
        FROM bright.group
        WHERE groupid=#groupid#
    </cfquery>

    <cfcontent  type="image/png" variable="#BinaryDecode(qryGetLogo.logo,'hex')#">

希望有人觉得这有帮助。

Don't know much to explain the real mechanics but this is what I found out.
The image gets stored differently. I opened up the returned images in a hex viewer. The postgresql text matched the mssql hex values from the 2nd byte to the end.

I tried everything to convert the postgresql output to no avail. It didn't help that the first byte was different.
In the end I changed the data type from bytea to text and saved the encoded binary file instead.

<cffile action="readbinary" file="#ExpandPath('./logo.png')#" variable="myLogo">
<cfquery name="saveLogo" datasource="#session.dsn#">
        UPDATE bright.group SET LOGO = 
        (<cfqueryparam value="#BinaryEncode(myLogo,'hex')#" cfsqltype='cf_sql_longvarchar'> )
    </cfquery>

<cfquery datasource="#session.dsn#" name="qryGetLogo">
        SELECT logo
        FROM bright.group
        WHERE groupid=#groupid#
    </cfquery>

    <cfcontent  type="image/png" variable="#BinaryDecode(qryGetLogo.logo,'hex')#">

Hope someone finds this helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文