1

我目前正在编写一个允许用户上传图像的脚本,只需几行简单的代码就可以了。无论如何,昨晚一切都像魅力一样运作,现在惊喜惊喜,奇怪的事情开始突然发生,不知道是什么原因。也就是说,重要的是要注意我正在使用jQuery Form Plugin异步传递值,并且该部分一直在完美地工作,因此我不会发布该部分代码。

这是cfc组件代码:

<cfcomponent output="false">  

<cffunction name="test" access="remote" output="false">
    <cfparam name="form.title" type="string" default="">
    <cfparam name="form.File" type="string" default="">

    <cfset add.Msg = ArrayNew(1)>

    <!---check if text is added and whether it contains more than 15 characters--->
    <cfif len (trim(form.title)) is 0>
       <cfset ArrayAppend(add.Msg,"plese enter some text")>
    <cfelseif len (trim(form.title)) lt 15>
       <cfset ArrayAppend(add.Msg,"text should be at least 15 characters long")> 
    </cfif>

    <!---check if text contains special characters--->
    <!--additionally we can allow other characters-->
    <cfif refind ("[^A-Z a-z 0-9\\+]+", form.title) and len (trim(form.title)) gte 15>
       <cfset ArrayAppend(add.Msg,"your post can not conatin special characters")>
    </cfif>   

    <!---check to see if file has been submited--->
    <cfif len(form.File) is 0>
       <cfset ArrayAppend(add.Msg,"you haven't selected a file")>
    </cfif>

    <!---if there are no errors try to upload file to temp--->
    <cfif ArrayLen (add.Msg) is 0>
        <cftry>
            <cffile action="upload" nameconflict="makeunique" destination="#GetTempDirectory()#" filefield="File">

            <!---check file size--->
            <cfif (CFFILE.FileSize GT (100 * 1024))>
                <cfset ArrayAppend(add.Msg,"#CFFILE.FileSize#you can not upload files bigger than 1MB")>
                <!---try to delete the file--->
                <cftry>
                    <cffile action="delete" file="#CFFILE.ServerDirectory#\#CFFILE.ServerFile#">
                <cfcatch>
                <!--we dan catch an error , though it would crash the page-->
                </cfcatch>
                </cftry>
             </cfif>   

            <!---check file extension with aditional layer of protection for just in case--->
            <cfif not ListFindnoCase("jpg,jpeg,png,gif",CFFILE.ServerFileExt) 
            or not isImageFile("#GetTempDirectory()##CFFILE.ServerFile#")>
                <cfset ArrayAppend(add.Msg,"#CFFILE.ServerFileExt#you can only upload jpg,png or gif images.")>

                <!---try to delete the file--->
                <cftry>
                    <cffile action="delete" file="#CFFILE.ServerDirectory#\#CFFILE.ServerFile#">
                <cfcatch>
                <!--again we can catch an error, though it would crash the page-->
                </cfcatch> 
                </cftry>
            </cfif>

        <cfcatch>
        <!--if there was an error we could log it, though that would crash the page-->
        </cfcatch>
        </cftry>
    </cfif>




   <cfif ArrayLen (add.Msg) is 0>


   <cfset ArrayAppend(add.Msg,"#CFFILE.ServerFile# - yay! success")>
   </cfif>




    <cfreturn serializeJSON(add.Msg)>
</cffunction>

正如您所看到的,这是一个应该有效的简单验证,并且正如我昨晚提到的那样有效。现在我遇到了文件大小和文件格式验证的问题。也就是说,无论我尝试什么,它都会返回大于 1M 的文件并且它不是正确的类型,即使我选择 300kb 文件、jpg 或 png 也是如此。如您所见,我在错误消息和文件中添加了#CFFILE.FileSize# 和#CFFILE.ServerFileExt#,它的大小小于1MB,并且类型正确。那么,WTF在这里进行吗?如果您对如何解决此问题或如何改进我的代码有任何建议,我将非常感谢您对此提供的帮助。

编辑

这是一个测试客户端代码

$(document).ready(function(){

var options = { 
beforeSend: function() { /* something will go here */ },
uploadProgress: function(event, position, total, percentComplete) { /* maybe in here */ },
    success: function(data) 
    {
        /*test */
        /*alert(data);
        return false;*/
    },
    complete: function(response) 
    {
        /* test */
        alert(response.responseText);
        return false;
    },
    error: function(error)
    {
        /*alert('error');
        return false;*/
    }

}; 

 $("#JqAjaxForm").ajaxForm(options);

});

和形式

<form id="JqAjaxForm" action="cfc/tests.cfc?method=test" method="post" enctype="multipart/form-data">
4

1 回答 1

0

我发现了一个问题,并且能够自己解决。无论如何,我最初的计算是不正确的,CFFILE.FileSize GT (100 * 1024) 不是 1MB 而是 100kb 所以它应该是 CFFILE.FileSize GT (1000 * 1024)。如果需要,请随意使用上面的代码。

于 2013-09-14T20:30:02.180 回答