此功能可将照片上传到我们网络的地图中。之后,它设置照片的路径并将源放入图像路径。
<cffunction name="uploadphoto" access="public" output="false" returntype="any">
<cfif isDefined("myForm.Submit")>
<returnvariable="imagePath">
</cfif>
<cfparam name="form.fileUpload" default="">
<cfif len(trim(form.fileUpload))>
<cfset LOCAL.imagePath = 'img/profiles/' />
<cfset strPath = ExpandPath( "./" ) & LOCAL.imagePath />
<cffile action="upload"
fileField="fileUpload"
destination="#strPath#"
nameconflict="overwrite">
<p>Thankyou, your file has been uploaded.</p>
</cfif>
<cfset form.filename = CFFILE.serverfile>
<cfreturn LOCAL.imagePath & form.filename/>
</cffunction>
This function update's the database and puts the img source path (imagePath) in the database.
<cffunction name="savephoto" access="public" output="false" returntype="any">
<cfargument name="dbname" type="any" required="no" default="#SERVER.cfc.core.getDBName()#" />
<cfargument name="myForm" type="Struct" required="yes">
<cfargument name="imagePath" type="any" required="yes">
<cfquery name="upload" datasource="#SERVER.cfc.core.getDSN()#">
UPDATE logboek.tbl_users SET photo=<cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.imagePath#"/>
WHERE Users_id=<cfqueryparam value="#SESSION.Login_logboek.User_id#" cfsqltype="cf_sql_integer">
</cfquery>
</cffunction>
<cffunction name="checkavatar" access="public" output="false" returntype="any">
<cfargument name="dbname" type="any" required="no" default="#SERVER.cfc.core.getDBName()#" />
<cfquery name="checkavatar" datasource="#SERVER.cfc.core.getDSN()#">
This function checks if the right user is logged in to see the photo.
SELECT photo FROM logboek.tbl_users WHERE Users_id=<cfqueryparam value="#SESSION.Login_logboek.User_id#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif checkavatar.RecordCount eq 1>
<cfreturn checkavatar.photo />
</cfif>
</cffunction>
我的问题在于视图我想使用图像源他从数据库中的路径输出屏幕上的图像VARIABLES.checkavatar需要返回&#34;照片&#34;从数据库中如下:
<cfoutput>
#VARIABLES.checkavatar#
</cfoutput>
答案 0 :(得分:0)
这里有几个问题。
首先,在函数checkavatar
中,您将变量checkavatar
重新分配给查询结果。实际上,这会在第一次调用时破坏该功能。由于CF的奇怪的范围规则,您需要在执行查询之前首先在函数中包含<cfset var checkavatar = -1>
之类的行。我还建议不要将变量命名为与它所在的函数相同的东西。
你对该函数的调用应该最终看起来像
<img src="#checkavatar()#"/>
因为该函数返回路径。您可能还有其他问题需要解决本地文件与文件的Web可访问路径之间的差异,但这很难远程调试。
接下来,删除<cfif checkavatar.RecordCount eq 1>
检查,或修改它以处理该情况。如果只是<cfreturn checkavatar.photo />
,如果找不到任何内容,那么你将得到的最差的是一个空字符串。目前,您将根本不返回任何内容,这将使调用它的页面崩溃。因此要么显式抛出,要么返回默认的头像图像。
最后,只使用传递给函数的变量作为参数(而不是通过session
传递变量,就像现在一样)。这是更好的设计,以后会更容易重用。
答案 1 :(得分:0)
<cffunction name="uploadphoto" access="public" output="false" returntype="any">
<cfif isDefined("myForm.Submit")>
<returnvariable="imagePath">
</cfif>
<cfparam name="form.fileUpload" default="">
<cfif len(trim(form.fileUpload))>
<cfset LOCAL.imagePath = 'img/profiles/' />
<cfset strPath = ExpandPath( "./" ) & LOCAL.imagePath />
<cffile action="upload"
fileField="fileUpload"
destination="#strPath#"
nameconflict="overwrite">
<p>Thankyou, your file has been uploaded.</p>
</cfif>
<cfset form.filename = CFFILE.serverfile>
<cfreturn LOCAL.imagePath & form.filename/>
</cffunction>
<cffunction name="savephoto" access="public" output="false" returntype="any">
<cfargument name="dbname" type="any" required="no" default="#SERVER.cfc.core.getDBName()#" />
<cfargument name="myForm" type="Struct" required="yes">
<cfargument name="imagePath" type="any" required="yes">
<cfquery name="upload" datasource="#SERVER.cfc.core.getDSN()#">
UPDATE logboek.tbl_users SET photo=<cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.imagePath#"/>
WHERE Users_id=<cfqueryparam value="#SESSION.Login_logboek.User_id#" cfsqltype="cf_sql_integer">
</cfquery>
</cffunction>
<cffunction name="checkavatar" access="public" output="false" returntype="any">
<cfargument name="dbname" type="any" required="no" default="#SERVER.cfc.core.getDBName()#" />
<cfquery name="checkavatar" datasource="#SERVER.cfc.core.getDSN()#">
SELECT photo FROM logboek.tbl_users WHERE Users_id=<cfqueryparam value="#SESSION.Login_logboek.User_id#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif checkavatar.RecordCount eq 1>
<cfreturn checkavatar.photo />
</cfif>
</cffunction>
<cfif IsDefined ("VARIABLES.checkavatar")>
<cfoutput>
<img src="#VARIABLES.checkavatar#"
</cfoutput>
<cfelse>
<cfoutput>
Error
</cfoutput>
</cfif>