Solved

Loop thru a multiple files

Posted on 2014-02-21
18
199 Views
Last Modified: 2014-02-26
Hi,

I need to loop thru the uploaded files and insert them into the database as an objects.


<cfset maxFiles = 3>
 
<cfquery name = "QLogFiles"  datasource='#strDSN#' username='#strUID#' password='#strPWD#'>

 <cfloop from="1" to="#maxFiles#" index="counter">

<cfif IsDefined('FORM.File_ + counter')>
INSERT INTO Impact_Log_Files (UploadedFile)
VALUES (

 <cfqueryparam cfsqltype="cf_sql_blob"
              value="#FileReadBinary(FORM.File_ + counter)#">
             
)
</cfif>
</cfloop>
</cfquery>
0
Comment
Question by:lulu50
  • 10
  • 8
18 Comments
 
LVL 25

Expert Comment

by:dgrafx
ID: 39879217
<cfif StructKeyExists(form,"file_" & counter)>
      INSERT INTO Impact_Log_Files
            (UploadedFile)
      VALUES
            ('#form["file_" & counter]#')
</cfif>
            
            or use
            ('#FileReadBinary(form["file_" & counter])#')
            if reading binary is what you want
0
 

Author Comment

by:lulu50
ID: 39886897
dgrafx,

can you please tell me how I am going to put it in <cfqueryparam

<cfqueryparam cfsqltype="cf_sql_blob"
              value='#FileReadBinary(form['file_' & counter])#'>,

I tried both ways but it did not work

the error:

The tag must be nested inside a cfquery tag.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39886997
you need to keep the code within your cfquery tag like you originally showed.
i simply supplied the meat of the problem.
see below
=======================================

First a note:
I advise that you get organized with single vs double quotes in the code you write.
Don't start off using single quotes (') and change to double quotes (") midstream or some variation of that.
Look at the code you first posted vs what I'm posting here.
Also, notice how you changed the double quotes I used in the code I provided for you to single - that won't work when you are using single quotes to set the value.
See this: <cfset xyz='don't use quotes like this'>
Do you see why this won't work?
It should be <cfset xyz="don't use quotes like this">
=======================================

<cfset maxFiles = 3>
<cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
      Set Nocount On;
      <cfloop from="1" to="#maxFiles#" index="counter">
            <cfif StructKeyExists(form,"file_" & counter)>
                  INSERT INTO Impact_Log_Files
                        (UploadedFile)
                  VALUES
                        (<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(form['file_' & counter])#">)
            </cfif>
      </cfloop>
</cfquery>

good luck ...
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:lulu50
ID: 39887009
in SQL the datatype for my field is this :


UploadedFile      varbinary(MAX)      Checked

Is this correct?
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39887039
Yes

Also
In ColdFusion Administrator I believe you need to enable binary large object retrieval (BLOB)
Thats in Advanced Settings when viewing the datasource.

If you are on hosted server where you don't have access to CF Administrator then ask your host about this if you are having issues.

NOTE: don't worry about this if you are not having issues!
0
 

Author Comment

by:lulu50
ID: 39887064
dgrafx,

my error now says that:

File '' does not exist.

<cfset maxFiles = 3>
<cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
      Set Nocount On;
      <cfloop from="1" to="#maxFiles#" index="counter">
            <cfif StructKeyExists(form,"file_" & counter)>
                  INSERT INTO Impact_Log_Files
                        (UploadedFile)
                  VALUES
                        (<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(form['file_' & counter])#">)    
                        )
            </cfif>
      </cfloop>
</cfquery>
0
 

Author Comment

by:lulu50
ID: 39887105
do I have to upload to the server first

or

I can just save it directly?
0
 

Author Comment

by:lulu50
ID: 39887136
hmmm

I just did this
<cfdump var="#form.file_1#">

just to see the value that I am passing

and it says it is an empty string
0
 
LVL 25

Accepted Solution

by:
dgrafx earned 500 total points
ID: 39887160
yes you need to upload to the server first ...

but you are now making me see that you need to rethink your concept.
what i coded was based off of your concept and to just give you the code that you asked for ...
anyway - entire solution (should be) below:

=================================
These are your 3 uploads on your form page

<cfloop from="1" to="3" index="ii">
      <INPUT TYPE="FILE" NAME="AttachFile#ii#" class="file" style="width:500px;"><br>
</cfloop>

=================================
This is your upload code on your action page that's placed before your insert to db code

<cfset filelocation="d:\websites\files\"><!--- example destination for uploads - change to your upload destination --->
<cfset maxFiles=3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
            <cftry>
            <cfif len(trim(Form["AttachFile" & ii]))>      
                  <CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
                  <cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>      
            </cfif>
            <cfcatch>
            <br>#cfcatch.message#<br>#cfcatch.detail#<br>
            #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
            </cfcatch></cftry>
      </cfloop>
      
<cfif len(trim(variables.Attachments))>
<cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
      Set Nocount On;
      <cfloop list="#variables.Attachments#" index="ii">
            <cfif FileExists(variables.filelocation & ii)>
                  INSERT INTO Impact_Log_Files
                        (UploadedFile)
                  VALUES
                        (<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(variables.filelocation & ii)#">)
            </cfif>
      </cfloop>
</cfquery>
</cfif>

Complete!
0
 

Author Comment

by:lulu50
ID: 39888553
dgrafx,

Thank you so much for the example.  Just one more thing before I close this question.

How can I call the binary file from my database after it got saved in the database. so, the user can download or save the file?

I can't thank you enough for all your help.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39888654
ok so your files are in the db
below i'm using download.png as a name for your image
for queryname.UploadedFile below - use your own query name
<cfheader
      name="content-disposition"
      value="inline; filename=download.png"
      />
<cfcontent
      type="image/*"
      variable="#queryname.UploadedFile#"
      />

==============

I have to ask though - WHY are you storing images as blobs in the DB?
Why not just save the image to disk with a reference to the image in the DB?
0
 

Author Comment

by:lulu50
ID: 39888705
company requirement -> security issues.

Thank you again,
lulu
0
 

Author Closing Comment

by:lulu50
ID: 39888707
Thank you
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39888711
ok so you'll need to delete the files after uploading them - right?
0
 

Author Comment

by:lulu50
ID: 39889229
dgrafx

are you still there

I thought I got it to work but it's not
0
 

Author Comment

by:lulu50
ID: 39889232
I'll open a new question for it
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39889241
what's not working?
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 39889243
post a link here to the new link
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need some regex help 15 29
Coldfusion graceful timeout 3 58
Coldfusion subreport - passing parameters 2 68
Coldfusion cfscript DELETE function 7 18
This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question