Solved

Loop thru a multiple files

Posted on 2014-02-21
18
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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