在搜索了许多博客文章、偶然发现并阅读了 Adobe 的文档后,似乎一致认为 CF10 不支持“多个”文件上传支持(除非您使用的是 flash 表单)。问题是,cffile 标记的“uploadall”值可能会上传所有文件,但不会返回有关文件的结果数组。
这是我整合的一个函数,它利用了底层 Java 方法并在 ACF 10 中进行了测试。
<cffunction name="getUploadedFiles" access="public" returntype="struct"
hint="Gets the uploaded files, grouped by the field name">
<cfset var local = {
files = {},
types = "text/plain,text/csv,application/msexcel,application/vnd.ms-excel,application/octet-stream",
tempFiles = form.getTempFiles(),
idx = 0} />
<cfscript>
arrayEach(form.getPartsArray(), function (field) {
var local = {fieldName = field.getName(), destinationFile = ""};
// Make sure the field available in the form is also
// available for the temporary files
if (structKeyExists(tempFiles, fieldName)) {
// Create the files of field array if it doesn't exist
if (!structKeyExists(files, fieldName)) {
files[fieldName] = [];
}
// If only 1 file was uploaded, it won't be an array - so make it one
if (!isArray(tempFiles[fieldName])) {
tempFiles[fieldName] = [tempFiles[fieldName]];
}
// Check that the form part is a file and within our list of valid types
if (field.isFile() && listFindNoCase(types, field.getContentType())) {
// Compile details about the upload
arrayAppend(files[fieldName], {
file = tempFiles[fieldName][++idx],
filePart = field,
filename = field.getFileName(),
filepath = field.getFilePath(),
contentType = field.getContentType(),
tempFile = tempFiles[fieldName][idx].getPath()
});
}
}
});
</cfscript>
<cfreturn local.files />
</cffunction>
与 cmets 一起,这只是循环所有表单部分,查找文件,并创建一个包含一些有用文件详细信息的数组(并根据我的应用程序要求按特定内容类型进行过滤)。
然后,我创建了 uploadFile 函数,该函数接受 fieldName 和 destinationPath 参数。我根据传入的字段获取上传文件的数组,遍历文件以确保目标文件路径不存在(如果存在则使其唯一),然后使用 的内容写入目标文件从临时上传引用的 java.io.File 对象。
<cffunction name="uploadFile" access="public" returntype="array"
hint="Uploads a file (or multiple files) from the form to the server">
<cfargument name="fieldName" type="string" required="true" />
<cfargument name="destinationPath" type="string" required="true" />
<cfset var local = {files = [], filepaths = [], allFiles = getUploadedFiles()} />
<cfif structKeyExists(local.allFiles, arguments.fieldName)>
<cfset local.files = local.allFiles[arguments.fieldName] />
</cfif>
<cfloop array="#local.files#" index="local.file">
<cfset local.file.destinationFile = arguments.destinationPath & local.file.fileName />
<cfif fileExists(local.file.destinationFile)>
<cfset local.file.destinationFile = listFirst(local.file.destinationFile, ".") & "_#getTickCount()#.csv" />
</cfif>
<cfset fileWrite(local.file.destinationFile, fileRead(local.file.file)) />
<cfset arrayAppend(local.filePaths, local.file.destinationFile) />
</cfloop>
<cfset setActiveFileName(local.filePaths[arrayLen(local.filePaths)]) />
<cfreturn local.filePaths />
</cffunction>
现在我可以完全控制所有上传的文件,并且可以处理需要的结果。