質問

I am currently working on a script that will allow users to upload images, nothing too sophisticated just a few simple lines of code. Anyway, last night everything was working like a charm and now surprise surprise, strange things started happening out of the blue for who knows what reason. That said, it's important to note that I am asynchronously passing values using jQuery Form Plugin and that part has been working flawlessly hence I will not post that part of the code.

here is the cfc component code:

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

as you can see it's a simple validation that should work and has worked as I mentioned last night. Now I am having issues with file size and file format validation. That said, whatever I try it returns that file is bigger that 1M and that it's not a correct type, even though I select 300kb file, jpg or png. as you can see i added #CFFILE.FileSize# and #CFFILE.ServerFileExt# to those to error msgs and file is there, it's less than 1MB in size and its correct type. So, WTF is going on here? I would really appreciate your help on this one, if you have any suggestions on how to fix this or how to improve my code.

EDIT

this is a testing client side code

$(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);

});

and form

<form id="JqAjaxForm" action="cfc/tests.cfc?method=test" method="post" enctype="multipart/form-data">
役に立ちましたか?

解決

I've found a problem and was able to sort it out on my own. Anyway, my initial calculation wasn't correct, CFFILE.FileSize GT (100 * 1024) is not 1MB but 100kb so it should be CFFILE.FileSize GT (1000 * 1024). Feel free to use the code above if you need it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top