Coldfusion Ajax Multi-File Upload - CFFile Overwriting ServerFile

纵饮孤独 提交于 2020-01-06 10:54:12

问题


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.

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.

Caller:

<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>

回答1:


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 :)



来源:https://stackoverflow.com/questions/23095985/coldfusion-ajax-multi-file-upload-cffile-overwriting-serverfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!