我要疯了,试图找到解决这个问题的方法。
这是代码:
<!--- Rename PNG File to remove _page_1.png --->
<cfset fileOnServer = "#RootDirectory#\calendar\#uploadedImage#">
<cfif FileExists("#fileOnServer#")>
FileExists<br>
<cfset test = GetFileInfo("#fileOnServer#")>
<cfdump var="#test#">
<cfset fileName = "#test.name#">
<cfset newFileName = #Replace(fileName,'_page_1','')#>
<cfset fullNewFilePath = "#RootDirectory#\calendar\#newFileName#">
<cfoutput>fileName: #fileName#<br>newFileName: #newFileName#<br>fullNewFilePath: #fullNewFilePath#<br></cfoutput>
<cftry>
<cffile action="rename" destination="#fullNewFilePath#" source="#fileOnServer#" attributes="normal" nameconflict="overwrite">
<cfcatch>
<cfoutput>
<div style="text-align: left;">
Error occured....<br /><br />
Message: <b>#cfcatch.Message#</b><br />
Detail: <b>#cfcatch.Detail#</b><br />
Type: <b>#cfcatch.Type#</b><br />
</div>
</cfoutput>
<cfset ErrorOccurred = "Y">
</cfcatch>
</cftry>
</cfif>
当它运行时,结果如下:
FileExists
struct
canRead YES
canWrite YES
isHidden NO
lastmodified {ts '2020-05-15 09:36:39'}
name January_2019_page_1.png
parent C:\Inetpub\vhosts\MyDomain\calendar
path C:\Inetpub\vhosts\MyDomain\calendar\January_2019_page_1.png
size 501168
type file
fileName: January_2019_page_1.png
newFileName: January_2019.png
fullNewFilePath: C:\Inetpub\vhosts\MyDomain\calendar\January_2019.png
Error occured....
Message: Attribute validation error for tag CFFILE.
Detail: The value of the attribute source, which is currently C:\Inetpub\vhosts\MyDomain\calendar\January_2019_page_1.png, is invalid.
Type: Application
我在页面前面有另一个 cffile action="rename" 执行没有问题。此外,如果在单独的页面上完成此代码将运行良好,而不是在此页面上(我需要它运行的地方)。
我的主机在 Windows 上运行 ColdFusion 10,如果有帮助的话。
感谢任何可以帮助解决此问题的人!
~~珍妮弗~~
* 更新以显示最终效果:*
<!--- Rename PNG File to remove _page_1.png --->
<cfset fileOnServer = "#RootDirectory#\calendar\#uploadedImage#">
<cfif FileExists("#fileOnServer#")>
<cfset RETRY_COUNT_MAX=10>
<cfset RETRY_SLEEP_MS=5000>
<cfloop index="retryCount" from="1" to="#RETRY_COUNT_MAX#">
<cftry>
<!--- Trying to repeat this code until it works --->
<cfset myFile = GetFileInfo("#fileOnServer#")>
<cfset fileName = "#myFile.name#">
<cfset newFileName = #Replace(fileName,'_page_1','')#>
<cfset fullNewFilePath = "#RootDirectory#\calendar\#newFileName#">
<cffile action="rename" destination="#fullNewFilePath#" source="#fileOnServer#" attributes="normal" nameconflict="overwrite">
<cfbreak>
<cfcatch>
<cfif retryCount GTE RETRY_COUNT_MAX><cfrethrow></cfif>
<cfthread action="sleep" duration="#RETRY_SLEEP_MS#" />
</cfcatch>
</cftry>
</cfloop>
</cfif>