Saving file to server in ColdFusion

2019-07-20 12:05发布

问题:

After doing some research I feel this should work, however it is not saving the file to my Images directory.

    <cfform name="uploadImgForm" method="post" action="#CGI.PATH_INFO#?#CGI.QUERY_STRING#" enctype="multipart/form-data">
    <input name="txtImg" type="file" />
    <input name="btnSubmit" type="submit" />
</cfform>
<cfif isDefined("Form.txtImg")>
    <cffile action="upload"
        fileField = "txtImg"
        destination="/Images"
        accept="image/jpeg"
        nameconflict="makeunique">
</cfif>

I plan on doing some validation, but I would like to get this simple example working first.

I came across this later which is helpful when trying to rename a file before upload: Adobe link

回答1:

The destination has to be a full path, otherwise it gets sent to a directory relative to the temp directory of ColdFusion.

Try this:

<cfset destination = expandPath("Images") />

<cffile action="upload"
    fileField = "txtImg"
    destination="#destination#"
    accept="image/jpeg"
    nameconflict="makeunique">


回答2:

Are there any error messages at all? Or, after posting, do you just get the form again?

Firstly, does the /Images directory exist under the default CF temp directory?

Secondly, try getting rid of the "accept" argument to the CFFILE, just in case your browser's sending an odd MIME type.