?
Solved

Loop thru a multiple files

Posted on 2014-02-21
18
Medium Priority
?
212 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 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