Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop thru a multiple files

Posted on 2014-02-21
18
Medium Priority
?
209 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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 …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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