首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当文件存在时Coldfusion cfftp putfile重命名

当文件存在时Coldfusion cfftp putfile重命名
EN

Stack Overflow用户
提问于 2020-08-01 10:13:31
回答 1查看 221关注 0票数 3

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

代码语言:javascript
复制
<cfftp
connection = "myConnection"
action = "putFile"
name = "uploadFile"
localFile = "C:\files\upload\some_file.txt"
remoteFile = "some_file.txt"
existing = "some_file.txt">

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

更新时间8/2/20:

这是我的cfm。

代码语言:javascript
复制
<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服务。

代码语言:javascript
复制
<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>

我已验证文件是否已成功上载到远程服务器。当我再次尝试使用相同的文件名进行上传时,系统仍然显示成功。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-08-01 11:34:27

您现有的代码将生成一个名为cfftp.succeeded的布尔变量。如果该变量的值为false,则可以执行任何您想做的操作。

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

编辑从此处开始

看完评论后,我想到了另一个选择。通过在文件名的末尾添加一些东西来使文件名唯一。UUID会一直起作用。如果您想要更短的时间,精确到毫秒的日期和时间也很可能一直有效

代码语言:javascript
复制
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>");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63201114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档