Coldfusion中的“图片预览”页面

银河的阿托

我知道如何将图像文件上传到服务器并将图像文件显示到页面。

但是,如果我想在确认页面上预览怎么办?

我可能会生成一个临时文件,该文件不会保存在数据库中,而是一个物理位置的文件,但是如果我决定单击“否”按钮,该怎么办。该临时文件将如何删除?

下面的代码将图像缩小并显示在页面上。但是它还会在目录中创建一个图像,在我单击“确定”或“否”后,该图像将保留在该图像中。NO是按钮,而OK自然是提交

    <!--- Make Temp Image as Preview --->
    <cfset mediapath = expandpath('../images/about')>
    <cfif structKeyExists(form,"image") and len(form.image)>
       <cffile action="upload"
       filefield="image"
       destination="#MediaPath#"
       nameconflict="makeunique">

       <cfimage name="uploadedImage"
       source="#MediaPath#/#file.serverFile#" >
       <cfset imagesize = 320>

        <cfif uploadedImage.width gt uploadedImage.height>
             <cfset percentage = (imagesize / uploadedImage.width)>
          <cfelse>
             <cfset percentage = (imagesize / uploadedImage.height)>
       </cfif>

       <cfset newWidth = round(uploadedImage.width * percentage)>
       <cfset newHeight = round(uploadedImage.height * percentage)>
      <!--- Show Image --->
    <cfoutput>

      <img src="../images/about/#file.serverFile#" style="height:#newHeight#; width:#newWidth#;">

我想我可能必须做URL传递或做某种CFScript。在返回按钮上的Onclick事件。

弗兰克·都铎

这是我按照您的意愿采取的方法。

好的,这可能超出了您的需求,但这应该可以使您在此图像删除问题上有所作为。

这是您的代码。但是我把它分成了两页。当然,我看不到该页面上还有其他内容以及您的表单处理内容,因此我们将从您给我们的内容开始。

<cfset mediapath = expandpath('../images/about')>

很好地使用structKeyExists():)

<cfif structKeyExists(form,"image") and len(form.image)>

在这里,我会暂停一下,建议您将上传内容限制为.gif,.jpeg,.jpg,.png(您的主要图片类型),以防止上传任何文件类型。我在下面添加了一个accept参数。

<cffile action="upload"
    filefield="image"
    destination="#MediaPath#"
    nameconflict="makeunique"
    accept="image/*">

<cfimage name="uploadedImage"
     source="#MediaPath#/#file.serverFile#" >

确定文件已上传,让我们分开关注

此时,我会问他们是否要预览文件。如果他们单击“预览”,则将他们带到preview.cfm页面。

然后,如果没有其他内容(例如ID或某些主键),则需要传递一个文件名。

因此,我从他们网站上的Adobe Coldfusion文档中删除了这个方便的脚本,并将使用该脚本将您的文件名转换为“其他”;)

<cfscript> 
theKey="123abc"; 
theKey=generateSecretKey("AES") 
superDuperSecretSoDontTell=encrypt(file.serverFile, theKey, "AES", "hex"); 
</cfscript> 

<a href="preview.cfm?fileName=#superDuperSecretSoDontTell#&theKey=#theKey#">Let's send this piggy!</a>

然后转到预览页面...

<cfoutput>   

(指向上方)这是pageA.cfm(指向下方)这是pageB.cfm

现在,我在下面提供了其余代码,让我们根据通过querystring变量/值对传递的信息来提取文件。

<cfscript> 
IWannaKnowTheSecret=decrypt(url.fileName, url.theKey, "AES", "hex"); 
</cfscript> 

在这里,我会暂停一下,建议您将上传内容限制为.gif,.jpeg,.jpg,.png(您的主要图片类型),以防止上传任何文件类型。我在下面添加了一个accept参数。

<cffile action="read" file="#MediaPath##IWannaKnowTheSecret#" variable="uploadedImage">

<cfset imagesize = 320>

有趣的地方在这里。因此,您将采用两个值中的较大者,并使用它来缩小大小。整洁的。

<cfif uploadedImage.width GT uploadedImage.height>
    <cfset percentage = (imagesize / uploadedImage.width)>
<cfelse>
    <cfset percentage = (imagesize / uploadedImage.height)>
</cfif>

<cfset newWidth = round(uploadedImage.width * percentage)>
<cfset newHeight = round(uploadedImage.height * percentage)>

然后执行如下形式:

<form action="" method="post" enctype="multipart/form-data">
Your current Image: 
<cfoutput>
Name: #uploadedImage#<br>
<img src="../images/about/#uploadedImage#" style="height:#newHeight#; width:#newWidth#;">
</cfoutput> 
Do you want to remove this?<br>
<input type="checkbox"
       name="removeImage" 
       value="1" />: Remove the logo image</cfif><br />

Wnat to replace your image?<br>
<input type="file" name="replacementImage">  (

<input type="hidden" 
       name="uploadedImage" 
       value="<cfoutput>#uploadedImage#</cfoutput>">

<input type="submit" value="submit" name="submit"       
</form>

<a href="#" onclick="javascript:window.history.back(-1);return false;">Cancel and go back</a>

如果他们继续操作并想要修复图像或更换它。提交然后您可以使用类似这样的东西。然后我们删除...

如果填充了replaceImage文件,则我们添加该文件。

那里有...

您正在分离一些担忧。您正在提供更好的选择。您允许更改或不更改。您正在给他们提供返回到您想要的任何地方的机会。

编辑:这是编码和解码内容的证明(如果您想使用它:

<cfscript> 
theKey="123abc"; 
theKey=generateSecretKey("AES"); 
superDuperSecretSoDontTell=encrypt("monkeytoots", theKey, "AES", "hex");  
</cfscript> 
<cfoutput><a href="?fileName=#superDuperSecretSoDontTell#&theKey=#theKey#">Let's send this piggy!</a></cfoutput>
<cfif isdefined("url.fileName") and isdefined("url.theKey")>
<cfscript> 
IWannaKnowTheSecret=decrypt(url.fileName, url.theKey, "AES", "hex"); 
</cfscript>

<cfoutput>
#IWannaKnowTheSecret#
</cfoutput>
</cfif>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章