Copy link to clipboard
Copied
Him I am using the following cffile to upload an image to a folder on the website, but wanted to know if there a way that it will rename the file according to the userID in the database. For example.... image name is jackson.jpg and Jackson's userID is 12, so i would like the image to be renamed to 12.jpg. Hope thats makes sense. I know this is a super simple upload system which is why i need help. Thanks
======= cffile code =============
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
destination="C:\ColdFusion9\wwwroot\myWebsite\user_images"
accept="image/jpg,image/jpeg">
<p>Thank you, your file has been uploaded.</p>
</cfif>
<cfform enctype="multipart/form-data" method="post">
<cfinput type="file" name="fileUpload" />
<cfinput type="submit" name="file_submit" value="Upload File" />
</cfform>
Can't figure out why it works in FF, but not IE.
| h1. The MIME type of the uploaded file image/pjpeg
Only files of type image/JPG,image/JPEG can be uploaded.
The reason is right there in the error message 😉 "image/Pjpeg" is not in the list of mime types you are accepting. Internet Explorer often has its own ideas about mime types.
That said, do not rely on the "accept" attribute. Mime types are easy to exploit:
Message was edited by: -==cfSearching==-
Copy link to clipboard
Copied
After you upload thefile, you can rename it by using this in Coldfusion using the action = rename of CFFILE..
or
I hope you can do that in Javascript also, while uploading you will be calling some function
to check whether its an acceptable format, in that function you could call the rename function.
<!--- Source Document is read-only but when renamed it becomes normal (not hidden or
read-only). --->
<cffile action = "rename" source = "c:\files\memo\readonlymemo.doc"
destination = "c:\files\memo\normalmemo.doc" attributes="normal">
Copy link to clipboard
Copied
Thanks for your post. How would i link the two?
Copy link to clipboard
Copied
No linking..
Its the two options you could pick anyone which is simple.
Try it and let me know.
Copy link to clipboard
Copied
Korrupt, try this:
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
destination="C:\ColdFusion9\wwwroot\myWebsite\user_images"
accept="image/jpg,image/jpeg" />
<cffile action="rename"
source=cffile.serverDirectory & cffile.serverFile
destination=cffile.serverDirectory & {newFileName} & cffile.serverFileExt />
<p>Thank you, your file has been uploaded.</p>
</cfif>
<cfform enctype="multipart/form-data" method="post">
<cfinput type="file" name="fileUpload" />
<cfinput type="submit" name="file_submit" value="Upload File" />
</cfform>
Replace {newFileName} with the variable holding the string of whatever you want the file name to be.
Just a heads up about about the cffile scope: when you use the cffile tag to upload a file to the server, the cffile scope is populated with data about the upload attempt. You can also specify a variable name to be filled with the data in the result attribute if you prefer. See the LiveDocs page on cffile action="upload".
Message was edited by: G. Taylor
Copy link to clipboard
Copied
Thanks for the posts. I got it to work in FF just fine and the way i want it to, but in IE i get this error and i don't know why.
Can't figure out why it works in FF, but not IE.
---------- Error message -----------
The MIME type of the uploaded file image/pjpeg was not accepted by the server. | |
Only files of type image/jpg,image/jpeg can be uploaded. Verify that you are uploading a file of the appropriate type. | |
The error occurred in C:\ColdFusion9\wwwroot\poll_system\admin_section\picture_upload.cfm: line 23 | |
21 : fileField="fileUpload" |
Copy link to clipboard
Copied
Can't figure out why it works in FF, but not IE.
| h1. The MIME type of the uploaded file image/pjpeg
Only files of type image/JPG,image/JPEG can be uploaded.
The reason is right there in the error message 😉 "image/Pjpeg" is not in the list of mime types you are accepting. Internet Explorer often has its own ideas about mime types.
That said, do not rely on the "accept" attribute. Mime types are easy to exploit:
Message was edited by: -==cfSearching==-
Copy link to clipboard
Copied
Thank you that fixed it.
Copy link to clipboard
Copied
accept="image/*"
can be quite handy as default for image files.
Copy link to clipboard
Copied
Yes, but not very safe.