Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 11th, 2008, 04:44 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default upload multiple files like gmail problem.

Hey Everyone,

Well this is my first time asking a question on here so please forgive me if i post
my question in the wrong section.

What i am trying to do is upload multiple files like gmail does. I found a script that does this on easycfm.com (Topic 13543).

But anyway when i try to upload multiple files it will create multiple records in my database (like it should), but it wont upload multiple files. What ever file i choose to upload first it will put in all the records added instead of each record having a different name.However,when i go to the destination of where the files go are suppose to go you will see all the files i uploaded,but in my database it has each record having the same name.

For example
if i upload 2 files. file one is called 1.txt and file two is called 2.txt. Instead of the first record being called1.txt and the other record being called 2.txt it will make both records have the file name as 1.txt. but in my destination folder it will have 1.txt and 2.txt

the only problem i can think of is. In the example there is no stored procedure an well i added my stored procedure into there action page because it will not insert anything into my database without the stored procedure.

here is what my stored procedure looks like (it is below on my action page as well).
Expand|Select|Wrap|Line Numbers
  1.  <cfquery name="attachment" datasource="CustomerSupport">
  2.     exec usp_CS_Insertattachments
  3.    '#Form.ID#','#Form.serialnum#','#Form.attachdescrip#','#Form.attachment1#',
  4. '#Form.fk_addedBy#','#Form.date_added#'
  5. </cfquery>
  6.  

But here is the full code

javascript that lets you upload multiple files

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.      var upload_number = 2;
  3.      function addFileInput()
  4.      {
  5.      var d = document.createElement("div");
  6.      var file = document.createElement("input");
  7.      file.setAttribute("type", "file");
  8.      file.setAttribute("name", "attachment"+upload_number);
  9.      d.appendChild(file);
  10.      document.getElementById("moreUploads").appendChild(d);
  11.      document.getElementById("totalAttachments").value = upload_number;
  12.      upload_number++;
  13.      }
  14. </script>
  15.  
here is the upload page

[HTML]<form action="userform.cfm" id="userForm" name="userForm" method="POST"
enctype="multipart/form-data">
<input type="file" name="attachment1" id="attachment" value="#attachment_ID_counter#"
onchange="document.getElementById('moreUploadsLink ').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;">
<input type="button" value="Attach another file"
onclick="javascript:addFileInput();" >
</div>
<input type="hidden" id="totalAttachments" name="totalAttachments" value="1">
<input type="submit" class="officalsubmit" value="submit" name="submit"
onClick="return validate_form();">
</form>
[/HTML]


here is my action page

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="attachment#counter#" destination=
  9. "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  10.               <cfquery name="attachment" datasource="CustomerSupport">
  11.     exec usp_CS_Insertattachments
  12.    '#Form.ID#','#Form.serialnum#','#Form.attachdescrip#','#Form.attachment1#',
  13. '#Form.fk_addedBy#','#Form.date_added#'
  14. </cfquery>
  15.      </cfif>
  16.      </cfloop>
  17. </cfif>
  18.  
Thanks in advance,
Rach

Last edited by acoder; August 12th, 2008 at 10:44 AM. Reason: Added [code] tags
Reply
  #2  
Old August 12th, 2008, 10:43 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

Welcome to Bytes!

In the query, you're using #form.attachment1# and not using the counter to differentiate between uploads.
Reply
  #3  
Old August 12th, 2008, 03:34 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

That worked! can't believe it was so simple. Thank you. I was wondering if you wouldn't mind answering another question. I am wondering instead of it putting whats in my file into my database. i was wondering how would i put the name of the file in my database? because right now its putting whats in my document into the database instead of the name of the file. I came up with this, but i am not sure how i would combine #counter# and #upload_file# into the cfquery.

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.           <CFSET UPLOAD_FILE=#FILE.SERVERFILE#>
  10.               <cfquery name="attachment" datasource="CustomerSupport">
  11.     exec usp_CS_Insertattachments
  12. '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#UPLOAD_FILE#','#Form.fk_addedBy#','#Form.date_added#'
  13. </cfquery>
  14.      </cfif>
  15.      </cfloop>
  16. </cfif>
  17.  
Thanks in advance,
Rach
Reply
  #4  
Old August 12th, 2008, 04:43 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

cffile.serverfile should be enough. That will contain the unique file name saved on the server.
Reply
  #5  
Old August 12th, 2008, 06:57 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Quote:
Originally Posted by acoder
cffile.serverfile should be enough. That will contain the unique file name saved on the server.
Acoder,

that worked for uploading one file with its file name (the code i have above). But when i try to upload multiple files it will only upload 1 file with its file name.

If i have #counter# instead of #upload_file# in my cfquery it will upload 2 files but not with there file name. Any way i can get counter and upload_file in cfquery together?

heres the code i have again
Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.           <CFSET UPLOAD_FILE=#file.serverfile#>
  10.               <cfquery name="attachment" datasource="CustomerSupport">
  11.     exec usp_CS_Insertattachments
  12. '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#UPLOAD_FILE#',
  13. '#Form.fk_addedBy#','#Form.date_added#'
  14. </cfquery>
  15.      </cfif>
  16.      </cfloop>
  17. </cfif>
Thanks in advance,
Rach
Reply
  #6  
Old August 13th, 2008, 10:21 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

Replace UPLOAD_FILE with cffile.serverfile. Since you're in a loop, after each upload, cffile.serverfile will contain the name of the file uploaded to the server. Which version of Coldfusion are you using?
Reply
  #7  
Old August 13th, 2008, 02:48 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

well i tried your idea an it puts the file name in there, but it wont let me upload multiple files. With cffile.serverfile in the cfquery it will only let me upload one file with its name. If i have #counter# in cfquery it will let me upload multiple files but wont let me put the name with it (which makes no sense to me). But as far as i know the version of coldfusion. The book i am using to help me out is 5.0, but i believe we have a newer version then that (but i got no clue what version that is).

but here is the updated code with cffile.serverfile in the cfquery
Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.               <cfquery name="attachment" datasource="CustomerSupport">
  10.     exec usp_CS_Insertattachments
  11. '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','
  12. #cffile.serverfile#','#Form.fk_addedBy#','#Form.date_added#'
  13. </cfquery>
  14.      </cfif>
  15.      </cfloop>
  16. </cfif>
i have also tried to take counter out of everything an when i did that it would let me upload 2 files.But for the first file uploaded it would give the correct file name, But then the 2nd file uploaded it renames the file completely (i am guessing trying to make it unique) although the 2nd file uploaded is a completely different name (the files i am using to test are named test1.txt and test2.txt).I think the code below would work if i could figure out what i am missing to it.Here is the code i tried without counter in it.

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="attachment">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment")>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="attachment" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.               <cfquery name="attachment" datasource="CustomerSupport">
  10.     exec usp_CS_Insertattachments
  11. '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#cffile.serverfile#',
  12. '#Form.fk_addedBy#','#Form.date_added#'
  13. </cfquery>
  14.      </cfif>
  15.      </cfloop>
  16. </cfif>
Thanks in advance,
Rach
Reply
  #8  
Old August 13th, 2008, 08:32 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

The cffile action="upload" tag is the one which uploads the file, not the cfquery tag.

The filefield attribute should have the name of the file, so use form.attachment#counter#.

To find out your Coldfusion version, check the Coldfusion Administrator, or, if you have debugging switched on, just check the debugging information at the bottom of each page.
Reply
  #9  
Old August 14th, 2008, 04:02 AM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

Well i really don't know how to get a hold of the Coldfusion Administrator (boss in charge of that) and the only time i see the anything about coldfusion that comes up is when i make an error so unless i make an error i don't really know how else to get it. But i think i am between 5-7.

But you know how i said i changed the code by taking counter out? well i deleted all the files in my database an now when i add files it will add multiple files but it will instead of give it the name test1.txt it will rename it to like an example ACF39DD.TXT. I don't know if that tells you anything but i thought i would tell you that. But that is with the new code, i went back to the old code based on what you said about the counter. But here is what i changed with what you told me, but i think i am missing something because i still cant get it right.

Like right now it will upload one file with the file name. But it just won't upload multiple, an only reason it was doing it before was because #counter# was in the cfquery. I was thinking that maybe the counter is not working right because it seems like it follows through when i upload the first file but it doesn't want to loop through the code again to add a second file. Maybe need to change how the cfloop works? like maybe changing it.just an idea on the top of my head. But here is what the cfquery looks like right now

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.  <cfquery name="attachment" datasource="CustomerSupport">
  10.     exec usp_CS_Insertattachments
  11. '#Form.ID#','#evaluate(serialnum)#','#Form.attachdescrip#','#cffile.serverfile#','#Form.fk_addedBy#','#Form.date_added#'
  12. </cfquery>
  13.      </cfif>
  14.      </cfloop>
  15. </cfif>
here is what the html looks like for it
Expand|Select|Wrap|Line Numbers
  1.  <input type="file" name="attachment" id="attachment" value="#attachment_ID_counter#" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
  2.           <div id="moreUploads"></div>
  3.           <div id="moreUploadsLink" style="display:none;">
  4.           <input type="button" value="Attach another file" 
  5.    onclick="javascript:addFileInput();" >
  6.           </div>
  7.           <input type="hidden" id="totalAttachments" name="totalAttachments" value="1">
but if you have any suggestions on where i could go from here i would really appreciate it because i am just lost. But thank you for all the help you have given me an if you got any suggestions let me know,

Thank you,
Rach
Reply
  #10  
Old August 14th, 2008, 09:06 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

Tell your boss to turn on debugging for you at least. That will contain a lot of useful information that will help you debug your application.

I've noticed that you've changed your HTML. It just has an input file element named "attachment", not "attachment1", "attachment2", etc. For the counter code to work, you will need the files to be named with 1, 2, 3, etc.
Reply
  #11  
Old August 14th, 2008, 02:32 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

The attachment part was the problem! It totally fixed it, THANK YOU THANK YOU!!! i know i have asked you a lot of questions,which thank you for answering them all you have no idea how much you have helped me! But i was wondering if you wouldn't mind answering just one more.

I was wondering how would i rename the files after i upload them? an the name i wanted to give each file uploaded was like this attach_ID_count attach being the name of the file i am uploading, the id coming from the form.id in the cfquery, and count being the file being uploaded.

This is what i have come up with so far but wasent sure how to give it the right name an i was going to put this right under the upload part.

Expand|Select|Wrap|Line Numbers
  1. <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\">
but THANK YOU so much for all the help you have given me you truley have saved me!
Rach
Reply
  #12  
Old August 14th, 2008, 04:56 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

You're welcome :)

For the renaming, add it to the destination field. Create a variable with the new name and then use that, e.g. something like
Expand|Select|Wrap|Line Numbers
  1. <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  2. <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
Reply
  #13  
Old August 14th, 2008, 07:19 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

All i can say is YOUR AWESOME! wow it works so beautiful i could cry. But THANK YOU so much for all the help :)

Rach

If anyone wants to see the finished action script code here it is.

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       verify the form field exists 
  6.      <cfif structKeyExists(FORM, "attachment"& counter)>
  7.            try and upload it ...
  8.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  9.           <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  10.           <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
  11.               <cfquery name="attachment" datasource="CustomerSupport">
  12.     exec usp_CS_Insertattachments
  13. '#Form.ID#','#evaluate(serialnum)#','#description#','#filename#','#Form.fk_addedBy#','#Form.date_added#'
  14. </cfquery>
  15.      </cfif>
  16.      </cfloop>
  17. </cfif>
  18.  
Reply
  #14  
Old August 14th, 2008, 08:15 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

No problem, you're welcome (don't cry too much ;))

Just a note that you should probably add some error checking to be on the safe side.

Post again back to the forum should you have any more questions.
Reply
  #15  
Old August 18th, 2008, 09:38 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Hey Everyone,

Well didn't mean to come back to this question. But there is something else i would like to add to my upload (well actually realized i needed). Well i want to be able to remove a file that is uploaded. Like every time you upload a file a remove button appears on the side so that if the user decides they don't want to upload it they don't have to. I tried doing this by myself. But i am having problems with my coldfusion part. The problem i am having is with my description. Basically lets say i try to upload 3 files. I decide i don't need the second file so i click remove an then click upload. This is the error i get.

An error occurred while evaluating the expression:
currentDescription = form["description" & counter]
The member "DESCRIPTION2" in dimension 1 of object "form" cannot be found.

here is my code

javascript
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   var upload_number = 2;
  3.      function addFileInput()
  4.      {
  5.      var d = document.createElement("div");
  6.      var l = document.createElement("a");
  7.      var file = document.createElement("input");
  8.      var text = document.createElement("input");
  9.      d.setAttribute("id", "f"+upload_number);
  10.      file.setAttribute("type", "file");
  11.      file.setAttribute("name", "attachment"+upload_number);
  12.      text.setAttribute("type", "text");
  13.      text.setAttribute("name", "description"+upload_number);
  14.      l.setAttribute("href", "javascript:removeFileInput('f"+upload_number+"');");
  15.      l.appendChild(document.createTextNode("Remove"));
  16.      d.setAttribute("id", "f"+upload_number);
  17.      d.appendChild(file);
  18.      d.appendChild(text);
  19.      d.appendChild(l);
  20.      document.getElementById("moreUploads").appendChild(d);
  21.      document.getElementById("totalAttachments").value = upload_number;
  22.      upload_number++;
  23.      }
  24.  
  25. function removeFileInput(i)
  26. {
  27.     var elm = document.getElementById(i);
  28.     document.getElementById("moreUploads").removeChild(elm);
  29.     upload_number = upload_number - 1;
  30. }
  31. </script>
form
Expand|Select|Wrap|Line Numbers
  1. <input type="file" name="attachment1" id="attachments" value="" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
  2.  Description <input type="text" name="description1" id="description" value="" /> 
  3.           <div id="moreUploads"></div>
  4.           <div id="moreUploadsLink" style="display:none;">
  5.           <input type="button" value="Attach another file" 
  6.    onclick="javascript:addFileInput();" >
  7.           </div>
  8.           <input type="hidden" id="totalAttachments" name="totalAttachments" value="1">
  9.  
coldfusion
Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.totalAttachments" default="0">
  4.      <cfloop from="1" to="#form.totalAttachments#" index="counter">
  5.       <cfset currentDescription = form["description" & counter]>
  6.       <!---verify the form field exists --->
  7.      <cfif structKeyExists(FORM, "attachment"& counter)>
  8.           <!--- try and upload it ...--->
  9.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  10.           <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  11.           <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
  12.               <cfquery name="attachment" datasource="CustomerSupport">
  13.     exec usp_CS_Insertattachments
  14. '#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
  15. </cfquery>
  16.      </cfif>
  17.      </cfloop>
  18. </cfif>
  19.  
Thanks in advance to anyone who can figure out my problem :)
Rach
Reply
  #16  
Old August 19th, 2008, 11:48 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

The problem is that when you remove the second one, you have a file input "f1" and file input "f3", so there's no f2 when you submit. One possible solution to this is to rename all lower inputs (3,4,5, etc.) with one less, so f3 becomes f2, f4 becomes f3, etc. and attachment3 becomes attachment2, description3 becomes description2, and so on.
Reply
  #17  
Old August 19th, 2008, 06:47 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Acoder,

I noticed that it was doing that yesterday when i was playing it doing f1,f2 and f3, an was thinking it had to do with the problem. i tried to take that part out of the whole thing but that didn't work to good. But how would i go about renaming it? i was looking at it yesterday an thinking it needed a count in there, but was not sure how to go about that in javascript.

Thank you again in advance,
Rach
Reply
  #18  
Old August 19th, 2008, 10:31 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

The basic idea is to use a loop which would loop from upload_Num_To_Remove+1 till total number of uploads. In our example of removing number 2 and having 3 file inputs, that would be only 3. In the loop, use setAttribute to change the name to one less, so 3 becomes 2.

This is now turning into a JavaScript question, so if you're still struggling with this, post a new thread in the JavaScript forum.
Reply
  #19  
Old August 19th, 2008, 10:56 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Hey Acoder,

yeah you are right this is more of a javascript problem then a coldfusion problem. I understand what your telling me about the loop, just having a hard time applying it. I posted my question over in the javascript forum an hope they can help me out.

But thank you for all your help acoder :),
Rach
Reply
  #20  
Old August 20th, 2008, 09:58 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

No problem. Good luck with your project!
Reply
  #21  
Old August 27th, 2008, 03:46 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Hey Acoder,

nope the thread is not over yet, which sorry for having so many threads. well i am still a bit lost on the server-side. i understand the changing the name from totalAttachments to uploads but the part about comma-delimited string has me thrown off. here is what i got

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "totalAttachments")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.uploads" default="0">
  4.      <cfloop from="1" to="#form.uploads#" index="counter">
  5.       <cfset currentDescription = form["description" & counter]>
  6.       <!---verify the form field exists --->
  7.      <cfif structKeyExists(FORM, "attachment"& counter)>
  8.           <!--- try and upload it ...--->
  9.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  10.           <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  11.           <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
  12.               <cfquery name="attachment" datasource="CustomerSupport">
  13.     exec usp_CS_Insertattachments
  14. '#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
  15. </cfquery>
  16.      </cfif>
  17.      </cfloop>
  18. </cfif>
Thank you :),
Rach
Reply
  #22  
Old August 27th, 2008, 04:41 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

I've split your post from the other thread and merged with this one instead since it's rather more appropriate to this thread and unrelated to the other one.

You can use cfloop over a list, so you're looking for something like:
Expand|Select|Wrap|Line Numbers
  1. <cfloop list="#form.uploads#" index="counter">
Reply
  #23  
Old August 27th, 2008, 06:56 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Hey Acoder,

it worked! yay it works, it works!!! you have no idea how excited i am lol :). But THANK YOU SO MUCH FOR ALL THE HELP!!! an sorry if i was to much trouble. but THANK YOU, THANK YOU,

Rach


if anyone wants the results to the coldfusion here it is. if you want the javascript and html for this you can find it at this link http://bytes.com/forum/showthread.ph...99#post3330599

Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "uploads")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.uploads" default="0">
  4.      <cfloop list="#form.uploads#" index="counter">
  5.       <cfset currentDescription = form["description" & counter]>
  6.       <!---verify the form field exists --->
  7.      <cfif structKeyExists(FORM, "attachment"& counter)>
  8.           <!--- try and upload it ...--->
  9.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  10.           <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  11.           <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
  12.               <cfquery name="attachment" datasource="CustomerSupport">
  13.     exec usp_CS_Insertattachments
  14. '#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
  15. </cfquery>
  16.      </cfif>
  17.      </cfloop>
  18. </cfif>
  19.  
Reply
  #24  
Old August 27th, 2008, 07:18 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,765
Default

LOL, yeah, it works.

Just one more thing that you should be aware of. You'll need to validate because you can never trust user input, so make sure the file has really been uploaded (and don't allow the user to upload just any file) before inserting info. into the database.
Reply
  #25  
Old August 28th, 2008, 05:27 PM
Site Addict
 
Join Date: Aug 2008
Location: United States
Posts: 695
Default

Hey Acoder,

Ran into one last problem (hope its the last problem). but when i tried earlier not to insert anything into the database i got this error


Expand|Select|Wrap|Line Numbers
  1.       Error processing CFFILE
  2.  
  3.       No data was received in the uploaded file '\' Saving empty (zero-length) files is prohibitted. Please make sure you specified the correct file.
heres the javascript
Expand|Select|Wrap|Line Numbers
  1.  var upload_number = 2;
  2.      function addFileInput()
  3.      {
  4.      var d = document.createElement("div");
  5.      var l = document.createElement("input");
  6.      var file = document.createElement("input");
  7.      var text = document.createElement("input");
  8.      var nbsp = document.createTextNode("\u00a0");
  9.      var space = document.createTextNode("\u00a0");
  10. <!---     var br = document.createElement("br");--->
  11.      var des = document.createTextNode("Description")
  12.      try {
  13.    file = document.createElement("<input type='file' name='attachment"+upload_number+"'>");
  14.    text = document.createElement("<input type='text' name='description"+upload_number+"'>");
  15.    l = document.createElement("<input type='button' onclick='javascript:removeFileInput("+upload_number+");'>");
  16. } catch (e) {
  17.   file = document.createElement("input");
  18.   text = document.createElement("input");
  19. }
  20.      d.setAttribute("id", "f"+upload_number);
  21.      file.setAttribute("type", "file");
  22.      file.setAttribute("name", "attachment"+upload_number);
  23.      text.setAttribute("type", "text");
  24.      text.setAttribute("name", "description"+upload_number);
  25.      l.setAttribute("id", "l"+upload_number);  
  26.      l.setAttribute("type", "button");
  27.      l.setAttribute("onclick", "javascript:removeFileInput("+upload_number+")");
  28.  
  29.      l.type="button";
  30.      l.value="Remove";
  31.      l.onclick="javascript:removeFileInput("+upload_number+")";
  32. <!---     l.setAttribute("href", "javascript:removeFileInput("+upload_number+");");--->
  33. <!---     l.appendChild(document.createTextNode("Remove"));--->
  34.      d.setAttribute("id", "f"+upload_number);
  35.      d.appendChild(file);
  36.      d.appendChild(nbsp);
  37.      d.appendChild(des);
  38. <!---     d.appendChild(br);--->
  39.       d.appendChild(space);
  40.      d.appendChild(text);
  41.      d.appendChild(l);
  42.      document.getElementById("moreUploads").appendChild(d);
  43.      document.getElementById("uploads").value += "," + upload_number;
  44.      upload_number++;
  45.      }
  46.  
  47. function removeFileInput(i)
  48. {
  49.     var elm = document.getElementById("f"+i);
  50.     document.getElementById("moreUploads").removeChild(elm);
  51. document.getElementById("uploads").value = document.getElementById("uploads").value.replace(i,"");
  52.  
  53. }
html
Expand|Select|Wrap|Line Numbers
  1.  <input type="file" name="attachment1" id="attachments" value="" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
  2.  Description <input type="text" name="description1" id="description" value="" /> 
  3.           <div id="moreUploads"></div>
  4.           <div id="moreUploadsLink" style="display:none;">
  5.           <input type="button" value="Attach another file" 
  6.    onclick="javascript:addFileInput();" >
  7.           </div>
  8.           <input type="hidden" name="uploads" id="uploads" value="1">
and coldfusion
Expand|Select|Wrap|Line Numbers
  1. <cfif structKeyExists(FORM, "uploads")>
  2.      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
  3.      <cfparam name="FORM.uploads" default="0">
  4.      <cfloop list="#form.uploads#" index="counter">
  5.       <cfset currentDescription = form["description" & counter]>
  6.       <!---verify the form field exists --->
  7.      <cfif structKeyExists(FORM, "attachment"& counter)>
  8.           <!--- try and upload it ...--->
  9.           <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
  10.           <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
  11.           <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
  12.               <cfquery name="attachment" datasource="CustomerSupport">
  13.     exec usp_CS_Insertattachments
  14. '#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
  15. </cfquery>
  16.      </cfif>
  17.      </cfloop>
  18. </cfif>
Thank you again for all your help,
Rach
Reply
  #26  
Old August 28th, 2008, 05:49 PM
acoder's Avatar