问题描述
我已经从此问题中获取了一些代码对于多文件上传,它可以正常工作,但cffile
会覆盖cffile.serverFile
,因此在parm
转储中,我得到的唯一文件名是上次上传的文件名,并非全部.
I've taken some code from this question for the multi-file upload, which works fine but the cffile
overwrites the cffile.serverFile
so in the parm
dump the only file name I get is the last one it uploaded, not all of them.
我尝试循环它,每次都添加一个数组,但是仍然是同样的问题.似乎cffile
一次上传了所有选择的文件.在这个问题中,OP说他使用正确的数据,但我不知道该怎么做,因为无论如何我都无法找回所有名称.
I tried looping it, appending an array each time, but still the same problem. It seems cffile
is uploading all files selected in one go. In this question the OP said he made a JSON response with the correct data but I have no idea how to do that since I can't get all the names back anyway.
呼叫者:
<script>
$(document).ready(function() {
$('#multiFileFrm').submit(function(event){
event.preventDefault();
$.each($('#multiFile')[0].files, function(i, file) {
var formData = new FormData();
formData.append('file-0', file);
ajaxUpload(formData);
});
});
function ajaxUpload(formData) {
console.log("ajaxUpload function called");
$.ajax({
type: 'POST',
url: "ajax/AJAX_multiUploadTest.cfm",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
$('#callback').html(data);
},
error: function(data) {
console.log(data);
}
});
}
});
</script>
<form method="post" enctype="multipart/form-data" id="multiFileFrm">
<input type="file" name="multiFile" id="multiFile" multiple="multiple" />
<input type="submit" id="submitFrm" value="Submit" />
</form>
<div id="callback"></div>
AJAX_multiUploadTest.cfm:
<cftry>
<cfobject component="code/profile" name="pro">
<cfset parm={}>
<cfset parm.userID=session.visitor.user.ID>
<cfset parm.database=application.datasource>
<cfset parm.form=form>
<cfset path="#application.directory#/data/dev/">
<cfif NOT DirectoryExists(path)><cfdirectory action="create" directory="#path#"></cfif>
<cffile action="upload" filefield="file-0" destination="#path#" nameConflict="makeUnique">
<cfset upload="#application.directory#/data/dev/#cffile.serverfile#">
<cfdump var="#parm#" label="parm" expand="no">
<cfcatch type="any">
<cfdump var="#cfcatch#" label="cfcatch" expand="no">
</cfcatch>
</cftry>
推荐答案
哇,通过阅读我自己的帖子,我发现了问题所在.从JavaScript在循环中调用该回调文件,因此每次它覆盖变量时.为了存储所有文件名,我只是简单地将它们存储在会话变量中:
Wow, by reading my own post I have figured out the problem. The callback file gets called in a loop from the JavaScript, so each time it overwrites the variables. In order to store all file names, I just simply stored them in a session variable:
<cfset arrayAppend(session.visitor.user.tempData, "#application.domain#data/dev/#cffile.serverfile#")>
然后输出所有上传的文件,并由其他功能处理后,也许要将其存储在数据库中,您只需清空数组即可!
This then outputs all files uploaded and once processed by other functions, maybe to store it in the database, you can just empty the array!
希望这对某人有帮助:)
Hope this helps someone :)
这篇关于Coldfusion Ajax多文件上传-CFFile覆盖ServerFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!