upload multiple files like gmail problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonneylake
    Contributor
    • Aug 2008
    • 769

    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,wh en 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).
    [CODE=cfm] <cfquery name="attachmen t" datasource="Cus tomerSupport">
    exec usp_CS_Insertat tachments
    '#Form.ID#','#F orm.serialnum#' ,'#Form.attachd escrip#','#Form .attachment1#',
    '#Form.fk_added By#','#Form.dat e_added#'
    </cfquery>
    [/CODE]


    But here is the full code

    javascript that lets you upload multiple files

    Code:
    <script type="text/javascript">
         var upload_number = 2;
         function addFileInput()
         {
         var d = document.createElement("div");
         var file = document.createElement("input");
         file.setAttribute("type", "file");
         file.setAttribute("name", "attachment"+upload_number);
         d.appendChild(file);
         document.getElementById("moreUploads").appendChild(d);
         document.getElementById("totalAttachments").value = upload_number;
         upload_number++;
         }
    </script>
    here is the upload page

    [HTML]<form action="userfor m.cfm" id="userForm" name="userForm" method="POST"
    enctype="multip art/form-data">
    <input type="file" name="attachmen t1" id="attachment " value="#attachm ent_ID_counter# "
    onchange="docum ent.getElementB yId('moreUpload sLink').style.d isplay = 'block';" />
    <div id="moreUploads "></div>
    <div id="moreUploads Link" style="display: none;">
    <input type="button" value="Attach another file"
    onclick="javasc ript:addFileInp ut();" >
    </div>
    <input type="hidden" id="totalAttach ments" name="totalAtta chments" value="1">
    <input type="submit" class="officals ubmit" value="submit" name="submit"
    onClick="return validate_form() ;">
    </form>
    [/HTML]


    here is my action page

    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">
                  <cfquery name="attachment" datasource="CustomerSupport">
        exec usp_CS_Insertattachments
       '#Form.ID#','#Form.serialnum#','#Form.attachdescrip#','#Form.attachment1#',
    '#Form.fk_addedBy#','#Form.date_added#'
    </cfquery>
         </cfif>
         </cfloop>
    </cfif>
    Thanks in advance,
    Rach
    Last edited by acoder; Aug 12 '08, 09:44 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to Bytes!

    In the query, you're using #form.attachmen t1# and not using the counter to differentiate between uploads.

    Comment

    • bonneylake
      Contributor
      • Aug 2008
      • 769

      #3
      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.

      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>
      Thanks in advance,
      Rach

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        cffile.serverfi le should be enough. That will contain the unique file name saved on the server.

        Comment

        • bonneylake
          Contributor
          • Aug 2008
          • 769

          #5
          Originally posted by acoder
          cffile.serverfi le 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
          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>
          Thanks in advance,
          Rach

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Replace UPLOAD_FILE with cffile.serverfi le. Since you're in a loop, after each upload, cffile.serverfi le will contain the name of the file uploaded to the server. Which version of Coldfusion are you using?

            Comment

            • bonneylake
              Contributor
              • Aug 2008
              • 769

              #7
              Acoder,

              well i tried your idea an it puts the file name in there, but it wont let me upload multiple files. With cffile.serverfi le 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.serverfi le in the cfquery
              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">
                            <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>
              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.

              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>
              Thanks in advance,
              Rach

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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.

                Comment

                • bonneylake
                  Contributor
                  • Aug 2008
                  • 769

                  #9
                  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

                  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="form.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#','#cffile.serverfile#','#Form.fk_addedBy#','#Form.date_added#'
                  </cfquery>
                       </cfif>
                       </cfloop>
                  </cfif>
                  here is what the html looks like for it
                  Code:
                   <input type="file" name="attachment" 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">
                  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

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    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 "attachment 1", "attachment 2", etc. For the counter code to work, you will need the files to be named with 1, 2, 3, etc.

                    Comment

                    • bonneylake
                      Contributor
                      • Aug 2008
                      • 769

                      #11
                      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.

                      Code:
                      <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

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        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[CODE=cfm]<cfset filename = cffile.ClientFi leName & "_" & form.id & "_" & counter & "." & cffile.ClientFi leExt>
                        <CFFILE ACTION="RENAME" SOURCE="C:\Inet pub\Development \WWWRoot\Rachel B\footprints\fo rm\attachments\ #CFFILE.ServerF ile#" destination="C: \Inetpub\Develo pment\WWWRoot\R achelB\footprin ts\form\attachm ents\#filename# ">[/CODE]

                        Comment

                        • bonneylake
                          Contributor
                          • Aug 2008
                          • 769

                          #13
                          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.

                          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="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
                                    <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
                                    <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
                                        <cfquery name="attachment" datasource="CustomerSupport">
                              exec usp_CS_Insertattachments
                          '#Form.ID#','#evaluate(serialnum)#','#description#','#filename#','#Form.fk_addedBy#','#Form.date_added#'
                          </cfquery>
                               </cfif>
                               </cfloop>
                          </cfif>

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            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.

                            Comment

                            • bonneylake
                              Contributor
                              • Aug 2008
                              • 769

                              #15
                              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:
                              currentDescript ion = form["descriptio n" & counter]
                              The member "DESCRIPTIO N2" in dimension 1 of object "form" cannot be found.

                              here is my code

                              javascript
                              Code:
                              <script type="text/javascript">
                                var upload_number = 2;
                                   function addFileInput()
                                   {
                                   var d = document.createElement("div");
                              	 var l = document.createElement("a");
                                   var file = document.createElement("input");
                              	 var text = document.createElement("input");
                              	 d.setAttribute("id", "f"+upload_number);
                                   file.setAttribute("type", "file");
                                   file.setAttribute("name", "attachment"+upload_number);
                              	 text.setAttribute("type", "text");
                                   text.setAttribute("name", "description"+upload_number);
                              	 l.setAttribute("href", "javascript:removeFileInput('f"+upload_number+"');");
                                   l.appendChild(document.createTextNode("Remove"));
                                   d.setAttribute("id", "f"+upload_number);
                                   d.appendChild(file);
                              	 d.appendChild(text);
                              	 d.appendChild(l);
                                   document.getElementById("moreUploads").appendChild(d);
                                   document.getElementById("totalAttachments").value = upload_number;
                                   upload_number++;
                                   }
                              
                              function removeFileInput(i)
                              {
                                  var elm = document.getElementById(i);
                                  document.getElementById("moreUploads").removeChild(elm);
                                  upload_number = upload_number - 1;
                              }
                              </script>
                              form
                              Code:
                              <input type="file" name="attachment1" id="attachments" value="" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
                               Description <input type="text" name="description1" id="description" value="" /> 
                                        <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">
                              coldfusion
                              Code:
                              <cfif structKeyExists(FORM, "totalAttachments")>
                                   <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
                                   <cfparam name="FORM.totalAttachments" default="0">
                                   <cfloop from="1" to="#form.totalAttachments#" index="counter">
                                    <cfset currentDescription = form["description" & counter]>
                                    <!---verify the form field exists --->
                                   <cfif structKeyExists(FORM, "attachment"& counter)>
                                        <!--- try and upload it ...--->
                                        <cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
                                        <cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
                                        <CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
                                            <cfquery name="attachment" datasource="CustomerSupport">
                                  exec usp_CS_Insertattachments
                              '#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
                              </cfquery>
                                   </cfif>
                                   </cfloop>
                              </cfif>
                              Thanks in advance to anyone who can figure out my problem :)
                              Rach

                              Comment

                              Working...