Join 118,692 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,164 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
However, i can not seem to get it to work on mine. What happens is it will insert multiple records into my database, but where the file goes it inserts the same file instead of 2 different files. Like if i insert file1.txt and file2.txt in both places it will say file1.txt. However, where my files are suppose to be stored you will see file1.txt and file2.txt.
<cfif structKeyExists(FORM, "totalAttachments")> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded"> <cfparam name="FORM.totalAttachments" default="0"> <cfloop from="1" to="#form.totalAttachments#" index="counter"> verify the form field exists <cfif structKeyExists(FORM, "attachment"& counter)> try and upload it ... <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE"> <cfquery name="attachment" datasource="CustomerSupport"> exec usp_CS_Insertattachments '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#Form.attachment1#','#Form.fk_addedBy#','#Form.date_added#' </cfquery> </cfif> </cfloop> </cfif>
an i also wanted to ask. when i upload my files how would i give it a different name. Like everytime i upload it the name would be like attachmentname_id_countofattachment. an each time i upload it gets a different id and different count.
However, i can not seem to get it to work on mine. What happens is it will insert multiple records into my database, but where the file goes it inserts the same file instead of 2 different files. Like if i insert file1.txt and file2.txt in both places it will say file1.txt. However, where my files are suppose to be stored you will see file1.txt and file2.txt.
<cfif structKeyExists(FORM, "totalAttachments")> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded"> <cfparam name="FORM.totalAttachments" default="0"> <cfloop from="1" to="#form.totalAttachments#" index="counter"> verify the form field exists <cfif structKeyExists(FORM, "attachment"& counter)> try and upload it ... <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE"> <cfquery name="attachment" datasource="CustomerSupport"> exec usp_CS_Insertattachments '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#Form.attachment1#','#Form.fk_addedBy#','#Form.date_added#' </cfquery> </cfif> </cfloop> </cfif>
an i also wanted to ask. when i upload my files how would i give it a different name. Like every time i upload it the name would be like attachmentname_id_countofattachment. an each time i upload it gets a different id and different count.
but where the file goes it inserts the same file instead of 2 different files. Like if i insert file1.txt and file2.txt in both places it will say file1.txt. However, where my files are suppose to be stored you will see file1.txt and file2.txt. ...
when i upload my files how would i give it a different name.
That is because the code is using the same field name '#Form.attachment1#' every time. The form field name should be dynamic. Based on the #counter# variable.
To rename the file try specifying the desired file name as part of the "destination" value. Another option is to "upload" then "rename" the file. Of course if a file by that name already exists, it will get renamed anyway because you are using nameconflict="MAKEUNIQUE".
But why are you using cfquery to execute a stored procedure instead of cfstoredproc?
This post has been edited by sansclue: 11 Aug, 2008 - 01:32 PM
Nice to hear from you again .Today figured out the counter part. An also figured out how to get the file name for each file i upload. But i am still baffled by the renaming of files.
here is my new code for the action page (everything else above is the same)
CODE
<cfif structKeyExists(FORM, "totalAttachments")> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded"> <cfparam name="FORM.totalAttachments" default="0"> <cfloop from="1" to="#form.totalAttachments#" index="counter"> verify the form field exists <cfif structKeyExists(FORM, "attachment"& counter)> try and upload it ... <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE"> <CFSET UPLOAD_FILE=#file.serverfile#> <cfquery name="attachment" datasource="CustomerSupport"> exec usp_CS_Insertattachments '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#UPLOAD_FILE#','#Form.fk_addedBy#','#Form.date_added#' </cfquery> </cfif> </cfloop> </cfif>
the first problem i am having is with the cfquery. If i have #UPLOAD_FILE# in my cfquery it will put the filename, but it wont insert multiple files (only one). If i put #counter# in the cfquery (where currently in the code #upload_file# is) it will insert multiple files but not put the filename for each file uploaded. An i don't know how to combine/ make those work together.
An well the second part i am having problems on is with the rename. The one thing i am having problems with it right now is, i don't know what file the person will be uploading until it is uploaded. Which means it could be a text file, word file, picture, anything. Like in this example (which i am uploading first and then a reloading). They know the name of there source file an i don't.
I have looked at lots of example online, but i cant seem to find one that helps me understand how to upload a file without knowing what the name of it will be.
Then the last problem i am having is when i rename it,it needs to be named something like this attach(name of attachment)_ID(gets id from cfquery form.ID)_count(count of attachment like if you upload 2 files first file will have 1 other file will have 2).But i am baffled on how to begin this as well.
If you have any suggestions on how i could fix these few problems i would really appreciate it.Sorry if this is confusing, hard to explain this problem after having to look at it for days. Ff need better explanation just ask an all try to explain it better.
attach(name of attachment)_ID(gets id from cfquery form.ID)_count(count of attachment like if you upload 2 files first file will have 1 other file will have 2).But i am baffled on how to begin this as well.
I don't really understand the problems you are having with the cfquery. But my suggestion would be to address the problems one at a time. It is much more difficult to debug multiple code problems at the same time. So comment out the cfquery for now and focus on the file uploading. Once that portion is working, move on to the query. Though you should be using cfstoredproc, not cfquery. Unless there is a good reason for it..
After you have uploaded a file inside your loop, you can access information about the saved file using the CFFILE structure. If you review the documentation, it has a complete list the values available.
http://livedocs.adobe.com/coldfusion/7/htmldocs/00000253.htm CFFILE.serverDirectory - Directory of the file saved on the server CFFILE.File - Filename of the file saved on the server CFFILE.FileName - Filename of the file saved on the server (without an extension) CFFILE.FileExt - Extension of the uploaded file on the server (without a period) ...
You can easily use those values to construct the "old" and "new" file names. Note, I am assuming (name of attachment) is the name of the file, without the extension:
Well i think i got "most" of the cfquery figured out. The problem was either i could upload multiple files an not have the files name (instead it puts whats in the files into the database an not the file name) or i could have the name of the file, but it would only upload one file.
Anyway i took the word "count" out of all of this an that has allowed me to upload multiple files. But now i am stuck on this. When i try to upload 2 files, for the first file it will put the file name, but for the second file it puts i guess i would call it a unique name for it like ACF39DD.TXT An it does this even though the files are 2 different names (the files i am using to test this is test1.txt and test2.txt). But here is the code with what i changed. If you got any suggestions on how i could fix this part it would very appreciated. It seems simple, but for some reason i just can't piece it together.
CODE
<cfif structKeyExists(FORM, "totalAttachments")> <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded"> <cfparam name="FORM.totalAttachments" default="0"> <cfloop from="1" to="#form.totalAttachments#" index="attachment"> verify the form field exists <cfif structKeyExists(FORM, "attachment")> try and upload it ... <cffile action="upload" fileField="attachment" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE"> <cfquery name="attachment" datasource="CustomerSupport"> exec usp_CS_Insertattachments '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#cffile.serverfile#','#Form.fk_addedBy#','#Form.date_added#' </cfquery> </cfif> </cfloop> </cfif>
an i am trying to not really focus on the rename part cause like you said its a lot to think about. Just hard to because seem so closely related. But this is what i had tried for the rename. i put this right under the cffile upload (its not in the code above because trying to focus on the uploading itself). But like i said not sure what to put in the destination. I tried to put just do a basic name like test behind it but it didn't even put the file in there. But i am going to try your idea after i get past the uploading part, but just thought i would show you what i have tried.
but if you got any suggestions on how i could handle the cfquery part of this it be much appreciate because i know its probably something simple, an i am just missing it.
But thank you again for all your help Sansclue Rach
When i try to upload 2 files, for the first file it will put the file name, but for the second file it puts i guess i would call it a unique name for it like ACF39DD.TXT An it does this even though the files are 2 different names (the files i am using to test this is test1.txt and test2.txt).
The files having different names doesn't matter. What matters is the whether the destination directory already contains a file by that same name. You cannot have two files with the same name in the same directory. So if you upload 'myFile.txt' to the same directory twice, CF will create a unique name for the second file. ie if you have been testing repeatedly with the same 5-6 files, chances are the destination directory already contains files with those names from previous attempts. Make sense?
If CF is generating names like 'ACF39DD.TXT' you must be using an older version. IIRC CF8 generates more user friendly names like: myFile1.txt, myFile2.txt, myFile3.txt .... etc. You could try using the CFFILE.Clientxxxx variables. In theory they should contain the original names of the files. Though I have never used them much.
This post has been edited by sansclue: 13 Aug, 2008 - 11:44 AM
Everything you say makes since, but still didn't work. I went an deleted all the files an instead of giving me test1.txt and test2.txt when i went to try an reupload it,it went an rewrote both files like i described to you with ACF39DD.TXT (but both different names of course). But although you say everytime i add a file that has the same name it should give a unique name,which your right it should be that way, but for some reason that is not the case. Everytime i uploaded just test1.txt (uploaded 1 file) it uploaded it with no problem an never was rewritten because i seen it in my table many times testing with the code above. But when i tried to upload 2 files at the same time it would keep the name for the first file uploaded but for the 2nd file uploaded it changed the name.
So i don't know what that tells me but it really don't make since. If you have any other suggestions it would be very much appreciated.
But thank you for all the help you have given me, Rach
Everything you say makes since, but still didn't work. I went an deleted all the files an instead of giving me test1.txt and test2.txt when i went to try an reupload it,it went an rewrote both files like i described to you with ACF39DD.TXT (but both different names of course). But although you say everytime i add a file that has the same name it should give a unique name,which your right it should be that way, but for some reason that is not the case. Everytime i uploaded just test1.txt (uploaded 1 file) it uploaded it with no problem an never was rewritten because i seen it in my table many times testing with the code above. But when i tried to upload 2 files at the same time it would keep the name for the first file uploaded but for the 2nd file uploaded it changed the name.
So i don't know what that tells me but it really don't make since. If you have any other suggestions it would be very much appreciated.
But thank you for all the help you have given me, Rach
I hear what you are saying, but that is next to impossible if you are uploading to the same directory and using "makeunique" ;-)
Are you actually checking at the physical files on the file system ... or are you basing this off of information in your database table?