gpt4 book ai didi

文件存在时 Coldfusion CFFTP putfile 重命名

转载 作者:行者123 更新时间:2023-12-04 04:00:06 26 4
gpt4 key购买 nike

使用cfftp上传文件时,如何判断文件是否已经存在并重命名?

<cfftp
connection = "myConnection"
action = "putFile"
name = "uploadFile"
localFile = "C:\files\upload\some_file.txt"
remoteFile = "some_file.txt"
existing = "some_file.txt">

cffile 标签有一个 makeunique 属性,这使得这个过程非常简单。谢谢。

20 年 8 月 2 日更新:

这是我的 cfm。

<cfif isDefined("Form.FileContents")>
<cfset localFileDirectory="c:\localfiles" />

<!--- If directory does not exist, create it --->
<cfif Not DirectoryExists(localFileDirectory)>
<cfdirectory action = "create" directory="#localFileDirectory#" />
</cfif>

<!--- Upload file to temp location --->
<cffile action = "upload"
fileField = "FileContents"
destination = "#localFileDirectory#"
nameconflict="overwrite"
result="tempFile">

<!--- Set the FTP file destination location (directory) --->
<cfset destinationPath = "remotefiles/myFiles" />
<!--- initialize attachment service --->
<cfset attachmentService = new ais.applications.components.service.aisdocdbFtpService()/>
<!--- upload file to ftp site --->
<cfset uploadResults = attachmentService.doUpload(destinationPath,tempFile)/>

<cfif uploadResults.Succeeded>
success
<cfelse>
fail
</cfif>
<cfelse>
<form method="post" action=<cfoutput>#cgi.script_name#</cfoutput>
name="uploadForm" enctype="multipart/form-data">
<input name="FileContents" type="file">

<input name="submit" type="submit" value="Upload File">
</form>
</cfif>

这是我正在使用的 FTP 服务..

<cfcomponent>
<cfproperty name="overwriteFiles" type="boolean" />
<cfproperty name="createDirectories" type="boolean" />

<cfset FTPProps = {server="myServer", port = "21", username="myUser", password="myPassword", stopOnError="yes"} >

<!--- Constructor --->
<cffunction name="init" access="public" output="false" returntype="ais.applications.components.service.aisdocdbFtpService" hint="constructor">
<cfargument name="overwriteFiles" type="boolean" required="false" default="false">
<cfargument name="createDirectories" type="boolean" required="false" default="true">
<cfset this.overwriteFiles = arguments.overwriteFiles/>
<cfset this.createDirectories = arguments.createDirectories/>
<cfreturn this/>
</cffunction>

<!--- doUpload --->
<cffunction name="doUpload" access="public" returntype="any" output="false">
<cfargument name="destinationPath" type="string" required="true"/>
<cfargument name="tempFile" type="any" required="true"/>

<cfset var sourcePath = "#tempFile.serverDirectory#"/>

<!--- FTP to AIS Doc DB --->
<cftry>
<!---Open the connection --->
<cfftp action="open" connection="myConnection" timeout="600" attributeCollection="#FTPProps#" />

<cfif this.createDirectories>
<cfset folders = ListToArray(arguments.destinationPath, '/')/>
<cfset var path = ''/>
<cfloop array="#folders#" index="folder" >
<cfset path = path & folder & '/'/>

<!--- Make Sure Directory Exsists --->
<cfftp action="existsdir" connection="myConnection" directory="#path#" result="directoryCheck"/>

<cfif !directoryCheck.returnValue>
<cfftp action="createdir" connection="myConnection" directory="#path#" result="createDirectoryResult"/>
</cfif>
</cfloop>
</cfif>

<!--- Upload File --->
<cfftp action="putfile" connection="myConnection" localfile="#sourcePath#\#tempFile.serverFile#" remoteFile="#destinationPath#/#tempFile.clientfile#" failIfExists="#!this.overwriteFiles#" transfermode="auto" result="uploadResult"/>
<cfcatch type="any" name="exception">
<cfrethrow/>
</cfcatch>
<cffinally>
<!--- Clean Up Temp Files --->
<cffile action = "delete" file = "#sourcePath#\#tempFile.serverFile#">
<!---close the connection --->
<cfftp action="close" connection="myConnection">
</cffinally>
</cftry>

<cfreturn uploadResult/>
</cffunction>
</cfcomponent>

我确认文件已成功上传到远程服务器。当我尝试使用相同的文件名第二次上传时,它仍然说它成功了。

谢谢。

最佳答案

您现有的代码将生成一个名为 cfftp.succeeded 的 bool 变量。如果该变量的值为 false,您可以做任何您想做的事。

或者,您可以运行 cfftp action = "listDir" 命令,这将生成一个查询对象,其中包含每个项目的名称,包括它是否是一个目录。您可以运行查询查询以查看文件是否存在。

编辑从这里开始

看完评论,我想到了另一个选择。通过在文件名末尾附加一些内容,使文件名无论如何都是唯一的。 UUID 将一直有效。如果您想要更短的时间,精确到毫秒的日期和时间也很可能一直有效

OriginalName = 'abc.txt';
FileNamePart = ListFirst(OriginalName, '.');
NewName1 = FileNamePart & DateTimeFormat(now(), 'yyyymmddHHnnssl');
NewName2 = FileNamePart & CreateUUID(); // could replace the hyphens with empty strings here.
Extension = ListLast(OriginalName, '.');
NewName = NewName2 & '.' & Extension; // could also use NewName1


writeoutput("filename is #FileNamePart#<hr>");
writeoutput("extension is #Extension#<hr>");
writeoutput("NewName1 is #NewName1#<hr>");
writeoutput("NewName2 is #NewName2#<hr>");
writeoutput("NewName is #NewName#<hr>");

关于文件存在时 Coldfusion CFFTP putfile 重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63201114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com