Solved

Loop thru a multiple files

Posted on 2014-02-21
18
197 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 24

Expert Comment

by:dgrafx
Comment Utility
<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
Comment Utility
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 24

Expert Comment

by:dgrafx
Comment Utility
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
 

Author Comment

by:lulu50
Comment Utility
in SQL the datatype for my field is this :


UploadedFile      varbinary(MAX)      Checked

Is this correct?
0
 
LVL 24

Expert Comment

by:dgrafx
Comment Utility
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
Comment Utility
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
Comment Utility
do I have to upload to the server first

or

I can just save it directly?
0
 

Author Comment

by:lulu50
Comment Utility
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 24

Accepted Solution

by:
dgrafx earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:lulu50
Comment Utility
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 24

Expert Comment

by:dgrafx
Comment Utility
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
Comment Utility
company requirement -> security issues.

Thank you again,
lulu
0
 

Author Closing Comment

by:lulu50
Comment Utility
Thank you
0
 
LVL 24

Expert Comment

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

Author Comment

by:lulu50
Comment Utility
dgrafx

are you still there

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

Author Comment

by:lulu50
Comment Utility
I'll open a new question for it
0
 
LVL 24

Expert Comment

by:dgrafx
Comment Utility
what's not working?
0
 
LVL 24

Expert Comment

by:dgrafx
Comment Utility
post a link here to the new link
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now