Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

uploading two files from a form in one go...?

New Here ,
Mar 27, 2010 Mar 27, 2010

Given i have two file input form values on one page, how would i upload them both on the second? This is what i have attempted to do..

FORM ON FIRST PAGE

<form onSubmit="return checkrequired(this)" action="upload2.cfm" method="post" enctype='multipart/form-data'>

Preset Name<br />
<input type=text name="requiredname" ><br>
Genre<br />
<input type=text name="requiredgenre" ><br>
BPM<br />
<input type=text name="requiredbpm" ><br>
Soundslike<br />
<input type=text name="requiredsoundslike" ><br>
Daw required<br />
<input type=text name="requireddaw" ><br>
VSTs required<br />
<input type=text name="requiredvst" ><br>
License<br />
<input type=text name="requiredlicense" ><br>
Instructions<br />
<textarea name="requiredinstructions" cols="20" rows="4" >
</textarea><br>
Preset file:<br/>
<input type='FILE' name='ofname' ><BR>
MP3 preview file:<br/>
<input type='FILE' name='ofname2' ><BR>
<BR>


<input type=submit value="Upload">
</form>

COLDFUSION ON SECOND PAGE


<cfset subdir=#GetCurrentTemplatePath()#>
<cfset subdir='#replace(subdir,'\upload2.cfm','\presets')#'>

<cfset subdir2=#GetCurrentTemplatePath()#>
<cfset subdir2='#replace(subdir2,'\upload2.cfm','\presetsmp3')#'>

<cfif ofname is ''>
<font color="Red" size="3"><b>Choose a file...!</b></font><br><br>
<a href="javascript:history.go(-1)" onMouseOver="self.status=document.referrer;return true"><b>BACK...</b></a>
<cfabort>
</cfif>


<cfset dstfile='#subdir#'>
<CFFILE ACTION="Upload"
    FILEFIELD="ofname"
    DESTINATION="#dstfile#"
    nameconflict="Skip">

<cfset dstfile='#subdir2#'>
<CFFILE ACTION="Upload"
    FILEFIELD="ofname2"
    DESTINATION="#dstfile#"
    nameconflict="Skip">

<cfquery name="newpreset" datasource="062409js06ag">
        insert into presets (artistid, artistname, presetname, presetgenre, bpm, soundslike, daws, vsts, license, pfilename, instructions) values (#findname.ID#, '#findname.uname#', '#requiredname#', '#requiredgenre#', '#requiredbpm#', '#requiredsoundslike#', '#requireddaw#', '#requiredvst#', '#requiredlicense#', '#serverfile#', '#requiredinstructions#')
        </cfquery>  

<CFOUTPUT><B>FILE RECEIVED!<BR></CFOUTPUT>

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Apr 03, 2010 Apr 03, 2010

iamrobot,

Have you figured this out, or do you still need some help?

Leonard B

Translate
Enthusiast ,
Mar 27, 2010 Mar 27, 2010

Here's the simple solution:

Use the result attribute of cffile tag.

Without the result attribute the serverfile variable etc. go into the CFFILE structure (it is really a good practice to write CFFILE.serverfile instead of just serverfile). When result is defined, for example, result="file1", you'll have result1.serverfile instead of CFFILE.serverfile.

With CF9 though, you can use <cffile action="uploadall"> , where you'll have an array of results. See CFML 9 pdf reference, page 212.

--

- Fernis - fernis.net - ColdFusion Developer For Hire

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 27, 2010 Mar 27, 2010

Ahh, but how would i input the names of both these files into a table in my database?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 27, 2010 Mar 27, 2010

Thanks for your "simple solution".. p.212 of the cfmlref doesn't tell me anything great,and <cffile action="uploadall"> means nothing to me out of context.

Im trying to upload two files (taken from one form) into separate locations and input both of their names (changed or unchanged) into separate tables in my database. Uploadall definitely doesn't do that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 28, 2010 Mar 28, 2010

If you need separate locations for the files, then you would use separate <cffile action="upload"> for each file, setting the destination correctly.

You obviously new to any databases as well?

While SELECT can return multiple rows, and UPDATE can modify multiple rows, an INSERT (without some subqueries)  typically inserts only a single row in the database. Thus, you need two separate INSERT queries, one for each file.

-Fernis

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 28, 2010 Mar 28, 2010

Hi iamrobot,

If I understand what you are trying to accomplish, you can handle this on

a single .cfm page. Take a look at the code structure below and see if

this will work for you. Of course you will most likely have to add more to

it, but this should give you the foundation to build from.

You can also use session instead of cookie, if you are using this in a

session enabled operation. I would also get use to using <cfqueryparam>

to. This is plenty of info online as to why.

I would also suggest that you restrict the type of files that a person can

upload to the server, by using the "accept" attribute of the "cffile tag"

Example .doc or pdf files, etc.

Leonard B

=== Code below here ===

<cfif IsDefined('form.btn_upload')>

    <cfset dir_path = GetDirectoryFromPath(ExpandPath("*.*"))>
   
    <!--- Begin processing first file submission --->
    <cfif IsDefined('form.file_one') AND Trim(form.file_one) neq "">
        <cffile
            action="upload"
            destination="#dir_path#folder_one\"
            filefield="file_one"
            nameconflict="overwrite">
       
            <cfset cookie.file_one = "#cffile.serverfile#">
            <cfset cookie.file_one_ext = "#cffile.serverfileext#">
   
        <cfquery name="rs_insert_1" datasource="dsn_here">
            Insert Into tbl_name
                (
                tbl_field
                , tbl_field
                )
                Values
                (
                nullif(<cfqueryparam value="#cookie.file_one#" cfsqltype="cf_sql_varchar">,'')
                , nullif(<cfqueryparam value="#cookie.file_one_ext#" cfsqltype="cf_sql_varchar">,'')
                )
        </cfquery>
                   
    </cfif>

    <!--- Begin processing second file submission --->
    <cfif IsDefined('form.file_two') AND Trim(form.file_two) neq "">
        <cffile
            action="upload"
            destination="#dir_path#folder_two\"
            filefield="file_two"
            nameconflict="overwrite">
       
            <cfset cookie.file_two = "#cffile.serverfile#">
            <cfset cookie.file_two_ext = "#cffile.serverfileext#">
   
        <cfquery name="rs_insert_2" datasource="dsn_here">
            Insert Into tbl_name
                (
                tbl_field
                , tbl_field
                )
                Values
                (
                nullif(<cfqueryparam value="#cookie.file_two#" cfsqltype="cf_sql_varchar">,'')
                , nullif(<cfqueryparam value="#cookie.file_two_ext#" cfsqltype="cf_sql_varchar">,'')
                )
        </cfquery>
   
    </cfif>

    <!--- Begin - Redirection to successful section --->
    <cflocation url="index.cfm?upload_status=Successful" addtoken="no">

<cfelseif IsDefined('url.upload_status') AND Trim(url.upload_status) eq "Successful">

    <!--- Begin successful upload section --->

    <cfoutput>
        <div style="clear: both">
        #cookie.file_one#<br />
        #cookie.file_one_ext#
        </div>
       
        <div style="clear: both; padding: 15px 0px 0px 0px">
        #cookie.file_two#<br />
        #cookie.file_two_ext#
        </div>
    </cfoutput>

<cfelse>

    <!--- Begin form elements below here --->

    <div style="clear: both">
        <cfform action="index.cfm" method="post" enctype="multipart/form-data" name="upload_1" id="upload_01" lang="en" dir="ltr">
            <div style="clear: both; padding: 10px 0px 0px 0px">
                <div><label for="file_one"> File One</label><br />
                <cfinput type="file" name="file_one" id="file_one" size="50px" style="width: 300px;" dir="ltr" lang="en"></div>
            </div>
       
            <div style="clear: both; padding: 10px 0px 0px 0px">
                <div><label for="file_one"> File Two</label><br />
                <cfinput type="file" name="file_two" id="file_two" size="50px" style="width: 500px;" dir="ltr" lang="en"></div>
            </div>
       
            <div style="clear: both; padding: 10px 0px 0px 0px">
                <div style="clear: both; float: left; padding: 0px 10px 0px 0px">
                <cfinput type="submit" name="btn_upload" id="btn_upload" style="letter-spacing: 1px; width: 125px; cursor: pointer;" dir="ltr" lang="en" value="UPLOAD"></div>
                <div style="float: left">
                <cfinput type="reset" name="btn_reset" id="btn_reset" style="letter-spacing: 1px; width: 125px; cursor: pointer;" dir="ltr" lang="en" value="CLEAR"></div>
            </div>
        </cfform>
    </div>

</cfif>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2010 Mar 29, 2010

...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2010 Mar 29, 2010

...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 30, 2010 Mar 30, 2010

yooo leonard help me out, the files have to be in different folders, however the names of these files and their extensions i want to keep in the same table... how do i do that???

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 03, 2010 Apr 03, 2010

iamrobot,

Have you figured this out, or do you still need some help?

Leonard B

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2010 Apr 04, 2010
LATEST

i've done it all now, i got the number wrong (thought it was in kilobytes) but as i was typing out the question i realised what i was doing wrong ( half the time i find the answer just by posing it in question form on a forum like this ) thanks, its just annoying that its a serverside check

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources