Coldfusion 表单和使用 cffile 上传图像

发布于 2024-09-14 07:14:54 字数 332 浏览 18 评论 0原文

我正在学习一些 Coldfusion,但我在为同事构建的这个基于小型表单的应用程序时遇到了麻烦。

这是第一页: http://pastebin.com/aLPYHPsF

如您所见,我想要什么要做的就是获取用户输入并将该输入输出到 html。文本内容工作正常,但我无法上传图像!我想要做的是让用户单击上传,获得确认,然后单击提交,然后将它们发送到生成的 html (cfm) 页面。页面上是他们上传的图像的调整大小版本。 请告诉我我做错了什么!现在,当单击“上传”时,表单就会转储。

I'm learning some Coldfusion, and I'm having trouble with this small form based application I'm building for a coworker.

Here is the first page: http://pastebin.com/aLPYHPsF

As you can see, what I want to do is get the user input and take that input and output it to html. The text stuff works fine, but I can't get the image to upload! What I want to do is have the user click upload, get a confirmation, then click submit and they are sent to the generated html (cfm) page. On the page is a resized version of the image they uploaded.
Please tell me what I'm doing wrong! Right now when "upload" is clicked, the form just dumps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-09-21 07:14:54

这是因为您忘记将 enctype="multipart/form-data" 分配给您的第一个 cfform。我编辑了你的一些代码。一探究竟。

<cfset strPath = ExpandPath( "./" ) />
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />

<table width="100%">
    <tr>
        <td align="center">

<cfform name="ecaform" action="ecagenerator.cfm" enctype="multipart/form-data">
<table style="font-family: arial; font-size: 9pt">
    <tr><td height="30px" align="center" colspan="2" style="background-color: #020058; color: #FFFFFF; font-family: arial;">
            <b>ECA Newsletter Creation Form</b>
        </td>
    </tr>
    <tr><td height="20"></td></tr>
    <tr>
        <td>Earned from:</td>
        <td>
            <cfinput
                    type="radio"
                    name="earnedfrom"
                    value="UC">
            UC
            <cfinput
                    type="radio"
                    name="earnedfrom"
                    value="ECA">
            ECA
        </td>
    </tr>
    <tr>
        <td>First Name:</td>
        <td>
            <cfinput
                    name="firstname">
        </td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td>
            <cfinput
                    name="lastname">
        </td>
    </tr>
    <tr>
        <td>Instructor's Name:</td>
        <td>
            <cfinput
                    name="instructorname">
        </td>
    </tr>
    <tr>
        <td>Date (MM/DD/YYYY):</td>
        <td>
            <cfinput
                    name="date">
        </td>
    </tr>
    <tr>
        <td>Sex:</td>
        <td>
            <cfinput
                    type="radio"
                    name="sex"
                    value="male">
            Male
            <cfinput
                    type="radio"
                    name="sex"
                    value="female">
            Female
        </td>
    </tr>
    <tr>
        <td>Certificate Type:</td>
        <td>
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Private">
            Private
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Recreational">
            Recreational
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Commercial">
            Commercial
        </td>

    </tr>
</table>

<table style="font-family: arial; font-size: 9pt; margin-top: 20 px;">
    <tr>
        <td>
        Upload the photo:
        </td>
    </tr>

    <tr>
        <td>
            <cfif isDefined("form.fileUpload")>
              <cffile action="upload"
                 fileField="fileUpload"
                 destination="#strPath#"
                 accept="image/*">
                <cfimage action="resize" 
                    width="200" 
                    height="200" 
                    source="#strPath##file.serverfile#"
                    destination="#strPath##file.serverfile#"
                    overwrite="yes">                 
                <img src="<cfoutput>#file.serverfile#</cfoutput>">
            </cfif>

        </td>
    </tr>
    <tr>
        <td>
            <form enctype="multipart/form-data" 
                method="post">
            <input type="file" 
                name="fileUpload" /><br /><br />
            <input type="submit" 
                value="Submit"
                action="ecagenerator.cfm" />
            </form>
        </td>
    </tr>

</table>
<table style="margin-top: 20px;">
    <tr>
            <td>
                <cfinput
                        type="submit"
                        name="Submit"
                        value="Submit">
            </td>
        </tr>
</table>

</cfform>
        </td>
    </tr>
</table>

It's because of you forgot to assign enctype="multipart/form-data" to your first cfform. I've edited some of your coding. Check it out.

<cfset strPath = ExpandPath( "./" ) />
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />

<table width="100%">
    <tr>
        <td align="center">

<cfform name="ecaform" action="ecagenerator.cfm" enctype="multipart/form-data">
<table style="font-family: arial; font-size: 9pt">
    <tr><td height="30px" align="center" colspan="2" style="background-color: #020058; color: #FFFFFF; font-family: arial;">
            <b>ECA Newsletter Creation Form</b>
        </td>
    </tr>
    <tr><td height="20"></td></tr>
    <tr>
        <td>Earned from:</td>
        <td>
            <cfinput
                    type="radio"
                    name="earnedfrom"
                    value="UC">
            UC
            <cfinput
                    type="radio"
                    name="earnedfrom"
                    value="ECA">
            ECA
        </td>
    </tr>
    <tr>
        <td>First Name:</td>
        <td>
            <cfinput
                    name="firstname">
        </td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td>
            <cfinput
                    name="lastname">
        </td>
    </tr>
    <tr>
        <td>Instructor's Name:</td>
        <td>
            <cfinput
                    name="instructorname">
        </td>
    </tr>
    <tr>
        <td>Date (MM/DD/YYYY):</td>
        <td>
            <cfinput
                    name="date">
        </td>
    </tr>
    <tr>
        <td>Sex:</td>
        <td>
            <cfinput
                    type="radio"
                    name="sex"
                    value="male">
            Male
            <cfinput
                    type="radio"
                    name="sex"
                    value="female">
            Female
        </td>
    </tr>
    <tr>
        <td>Certificate Type:</td>
        <td>
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Private">
            Private
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Recreational">
            Recreational
            <cfinput
                    type="radio"
                    name="certtype"
                    value="Commercial">
            Commercial
        </td>

    </tr>
</table>

<table style="font-family: arial; font-size: 9pt; margin-top: 20 px;">
    <tr>
        <td>
        Upload the photo:
        </td>
    </tr>

    <tr>
        <td>
            <cfif isDefined("form.fileUpload")>
              <cffile action="upload"
                 fileField="fileUpload"
                 destination="#strPath#"
                 accept="image/*">
                <cfimage action="resize" 
                    width="200" 
                    height="200" 
                    source="#strPath##file.serverfile#"
                    destination="#strPath##file.serverfile#"
                    overwrite="yes">                 
                <img src="<cfoutput>#file.serverfile#</cfoutput>">
            </cfif>

        </td>
    </tr>
    <tr>
        <td>
            <form enctype="multipart/form-data" 
                method="post">
            <input type="file" 
                name="fileUpload" /><br /><br />
            <input type="submit" 
                value="Submit"
                action="ecagenerator.cfm" />
            </form>
        </td>
    </tr>

</table>
<table style="margin-top: 20px;">
    <tr>
            <td>
                <cfinput
                        type="submit"
                        name="Submit"
                        value="Submit">
            </td>
        </tr>
</table>

</cfform>
        </td>
    </tr>
</table>
萌梦深 2024-09-21 07:14:54

您可能需要考虑在图像上传部分使用 。基本上,您希望用于图像上传的表单位于 内,以便通过 ajax 提交表单。将图像表单操作发布到使用 的 PreviewImage.cfm,使用 isImageFile() 验证它是否是图像文件,然后使用 < ;cfimage> 调整其大小并显示缩略图(可以使用 action="writetobrowser")。如果您不想编写额外的 JavaScript 代码,我想将上传的图像与父表单关联起来的最简单方法是将文件路径存储在会话中。

You might want to look into using <cfdiv> for the image upload section. Basically you want the form for image uploading be inside the <cfdiv>, so that the form is submitted through ajax. Have the image form action post to a previewImage.cfm that uses <cffileupload>, validate with isImageFile() if it is an image file, then use <cfimage> to resize it and display a thumbnail (may use action="writetobrowser"). The easiest way to associate the uploaded image with the parent form would be storing the file path in Session I guess, if you don't want to code up extra Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文