使用ColdFusion将上传的图像保存到服务器
我被我认为创建功能以允许用户将图片上传到服务器目录的相对简单的任务所困扰,并且正在寻求一些帮助。
我有一个带有简单界面的简单表单,传递一个名为“imagedata”的变量,它是从文件输入传递的数据,一个名为“name”的变量,它是指定的图像名称,以及一个名为“imagetype”的变量,它传递一个文件扩展名。
我在函数(处理程序)中使用的用于保存此文件的代码如下:
<cfset form.imageName = form.name & "." & form.imagetype />
...
<cffile action="write" file="#sitepath#storage/bannerImages/#form.imagename#" output="#form.imageData#" />
这显然不起作用;它将使用正确的名称保存文件,但不会正确保存文件并且不会渲染为图像。我认为这是相对简单的事情,而且我认为我忽略了一些东西,因为我在谷歌上没有找到任何东西,这很奇怪。帮助解决这个问题将不胜感激。
I'm being stymied by what I thought would be the relatively simple task of creating functionality to allow users to upload a picture to a server directory and am looking for some help.
I have a simple form with a straightforward interface, passing a variable named 'imagedata' which is the data passed from a file input, a variable named 'name' which is a specified image name, and a variable named 'imagetype' which passes a file extension.
The code I'm using in the function (handler) to save this file is as follows:
<cfset form.imageName = form.name & "." & form.imagetype />
...
<cffile action="write" file="#sitepath#storage/bannerImages/#form.imagename#" output="#form.imageData#" />
This apparently won't work; it will save a file with the correct name, but does not save the file correctly and doesn't render as an image. I would think that this is something relatively simple, and I would think I've overlooked something because I'm not finding anything on google, which is odd. Help with this problem would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
action="write"
,使用action="upload"
。示例:
这会将文件放置在
/tmp/uploads
中(例如,在 Windows 上使用d:/tmp/uploads
) - 这是一个无法在线访问的目录。此时,您必须验证该文件是否如其所声称的那样 - 具有适当尺寸和文件大小的图像文件,而不是屏蔽的 EXE 或 嵌入 ZIP。
一旦您确信该文件可以安全地托管在您的服务器上,您就可以将其移动到适当的目录并将名称设置为所选的名称。
类似...
该代码未经测试,并且 - 与您在服务器上运行的所有内容一样,尤其是涉及客户端上传的代码 - 确保您在使用它之前了解它在做什么。
Adobe LiveDocs 和 各种CF博客值得花一些时间消化。
Don't use
action="write"
, useaction="upload"
.Example:
This will place the file in
/tmp/uploads
(use for exampled:/tmp/uploads
on Windows) - which is a directory not accessible online.At this point, you must verify that the file is what it claims to be - an image file of appropriate dimensions and filesize, and not a masked EXE nor embedded ZIP.
Once you are happy the file is safe for hosting on your server, you can then move it to the appropriate directory and set the name to what was chosen.
Something like...
That code is untested, and - as with everything you run on your servers, but especially so with code involving client uploads - make sure you understand what it is doing before using it.
There is plenty of valuable information on Adobe LiveDocs and on the various CF blogs that is worth spending some time digesting.