Greetings -
I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to mydirectory, but for some reason, I am getting different file names on the database.
Here is the full code: please do serach on kewword "database" to see where I added my database code - that where I need help with.
And here the link where I got it from: http://www.bennadel.com/blog/1117-As...ColdFusion.htm
Thank you so much for any input you can provide
@Karim - <!---
-
Set the number of files that can uploaded in a single
-
form submission.
-
--->
-
<cfset REQUEST.FileCount = 5 />
-
-
-
<!--- Set the destination folder for uploads. --->
-
<cfset REQUEST.UploadPath = ExpandPath( "./uploads/" ) />
-
-
-
<!--- Param the appropriate number of file fields. --->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<!--- Param file value. --->
-
<cfparam
-
name="FORM.file#intFileIndex#"
-
type="string"
-
default=""
-
/>
-
-
-
</cfloop>
-
-
-
-
-
<!--- Param upload flag. --->
-
<cftry>
-
<cfparam
-
name="FORM.submitted"
-
type="numeric"
-
default="0"
-
/>
-
-
-
<cfcatch>
-
<cfset FORM.submitted = 0 />
-
</cfcatch>
-
</cftry>
-
-
-
-
-
<!--- Set up an array to hold errors. --->
-
<cfset arrErrors = ArrayNew( 1 ) />
-
-
-
-
-
<!--- Check to see if the form has been submitted. --->
-
<cfif FORM.submitted>
-
-
-
<!---
-
Here is where we would validate the data; however,
-
in this example, there really isn't anything to
-
validate. In order to validate something, we are going
-
to require at least one file to be uploaded!
-
--->
-
-
-
-
-
<!---
-
Since we are going to require at least one file, I am
-
going to start off with an error statement. Then, I am
-
gonna let the form tell me to DELETE IT.
-
--->
-
<cfset ArrayAppend(
-
arrErrors,
-
"Please select at least one file to upload"
-
) />
-
-
-
-
-
<!--- Loop over the files looking for a valid one. --->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<cfif Len( FORM[ "file#intFileIndex#" ] )>
-
-
-
<!--- Clear the errors array. --->
-
<cfset ArrayClear( arrErrors ) />
-
-
-
<!--- Break out of loop. --->
-
<cfbreak />
-
-
-
</cfif>
-
-
-
</cfloop>
-
-
-
-
-
<!---
-
Check to see if there were any form validation
-
errors. If there are no errors, then we can continue
-
to process the form. Otherwise, we are going to skip
-
this and just let the page render again.
-
--->
-
<cfif NOT ArrayLen( arrErrors )>
-
-
-
<!---
-
Create an array to hold the list of uploaded
-
files.
-
--->
-
<cfset arrUploaded = ArrayNew( 1 ) />
-
-
-
-
-
<!---
-
Loop over the form fields and upload the files
-
that are valid (have a length).
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<!--- Check to see if file has a length. --->
-
<cfif Len( FORM[ "file#intFileIndex#" ] )>
-
-
-
<!---
-
When uploading, remember to use a CFTry /
-
CFCatch as complications might be encountered.
-
--->
-
<cftry>
-
<cffile
-
action="upload"
-
destination="#REQUEST.UploadPath#"
-
filefield="file#intFileIndex#"
-
nameconflict="makeunique"
-
/>
-
-
-
<!---
-
Store this file name in the uploaded file
-
array so we can reference it later.
-
--->
-
<cfset ArrayAppend(
-
arrUploaded,
-
(CFFILE.ServerDirectory & "\" & CFFILE.ServerFile)
-
) />
-
-
<!--- Store file names in the database --->
-
<CFQUERY NAME="CreateDocumentRecord" DATASOURCE="mydb">
-
INSERT INTO Uploads (user_ID, File_Name, Date_Published)
-
VALUES (#Form.ClientID#, 'file#intFileIndex#', #Now()#)
-
</CFQUERY>
-
-
-
-
<!--- #FILE.ClientFile# #intFileIndex# File_Name, #intFileIndex#--->
-
-
-
<!--- Catch upload errors. --->
-
<cfcatch>
-
-
-
<!--- Store the error. --->
-
<cfset ArrayAppend(
-
arrErrors,
-
"There was a problem uploading file ###intFileIndex#: #CFCATCH.Message#"
-
) />
-
-
-
<!---
-
Break out of the upload loop as we
-
don't want to deal with any more
-
files than we have to.
-
--->
-
<cfbreak />
-
-
-
</cfcatch>
-
</cftry>
-
-
-
</cfif>
-
-
-
</cfloop>
-
-
-
-
-
<!--- Check to see if we have any form errors. --->
-
<cfif ArrayLen( arrErrors )>
-
-
-
-
-
<!---
-
We encountered an error somewhere in the upload
-
process. As such, we want to clean up the server
-
a bit by deleteing any files that were
-
successfully uploaded as part of this process.
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#ArrayLen( arrUploaded )#"
-
step="1">
-
-
-
<!--- Try to delete this file. --->
-
<cftry>
-
<cffile
-
action="delete"
-
file="#arrUploaded[ intFileIndex ]#"
-
/>
-
-
-
<cfcatch>
-
<!--- File could not be deleted. --->
-
</cfcatch>
-
</cftry>
-
-
-
</cfloop>
-
-
-
-
-
<cfelse>
-
-
-
-
-
<!---
-
!! SUCCESS !!
-
The files were properly uploaded and processed.
-
Here is where you might forward someone to some
-
sort of success / confirmation page.
-
--->
-
-
-
-
-
</cfif>
-
-
-
</cfif>
-
-
-
</cfif>
-
-
-
-
-
<!--- Set the content type and reset the output buffer. --->
-
<cfcontent
-
type="text/html"
-
reset="true"
-
/>
-
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html>
-
<head>
-
<title>Multiple File Uploads</title>
-
</head>
-
<body>
-
-
-
<cfoutput>
-
-
-
<h1>
-
Multiple File Upload ColdFusion Example
-
</h1>
-
-
-
-
-
<!--- Check to see if we have any errors to display. --->
-
<cfif ArrayLen( arrErrors )>
-
-
-
<p>
-
Please review the following errors:
-
</p>
-
-
-
<ul>
-
<cfloop
-
index="intError"
-
from="1"
-
to="#ArrayLen( arrErrors )#"
-
step="1">
-
-
-
<li>
-
#arrErrors[ intError ]#
-
</li>
-
-
-
</cfloop>
-
</ul>
-
-
-
</cfif>
-
-
-
-
-
<form
-
action="#CGI.script_name#"
-
method="post"
-
enctype="multipart/form-data">
-
-
-
<!--- Submission flag. --->
-
<input type="hidden" name="ClientID" value="163">
-
<input type="hidden" name="submitted" value="1" />
-
-
-
-
-
<!---
-
Loop over the number of files we are going to
-
allow for the upload.
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<label for="file#intFileIndex#">
-
File #intFileIndex#:
-
</label>
-
-
-
<input
-
type="file"
-
name="file#intFileIndex#"
-
id="file#intFileIndex#"
-
/>
-
-
-
<br />
-
-
-
</cfloop>
-
-
-
-
-
<input type="submit" value="Upload Files" />
-
-
-
</form>
Use CFFILE.ServerFile instead of file#intFileIndex# because file#intFileIndex# is the name of the file field in the form, not the uploaded file name.
4 4757
Greetings - ATTN Bytes Admin: My apologies, I am new to your Forum, I posted this thread on other place by accident, I tried to delete it but I couldn't, please delete the other one when you have a chance. Thank you. Back to my thread:
I saw this code (pls see link below or code is pasted blow as well) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to mydirectory, but for some reason, I am getting different file names on the database.
exp: when I ploaded this files: photo1 photo2 photo3 photo4, I see all of them with right files names photo1.jpg photo2.jpg photo3.jpg photo4.jpg on the upload folder, but they get renamed on the database to: file1 file2 file3 file4. I'd like to see the same file names on the database as well.
Below is the full code: Please do serach on kewword "database" to see where I added my database code - that's where I need help with - everything else is working fine.
And here the link where I got it from: http://www.bennadel.com/blog/1117-As...ColdFusion.htm
Thank you so much for any input you can provide
@Karim
MoroccoUSA.com - <!---
-
Set the number of files that can uploaded in a single
-
form submission.
-
--->
-
<cfset REQUEST.FileCount = 5 />
-
-
-
<!--- Set the destination folder for uploads. --->
-
<cfset REQUEST.UploadPath = ExpandPath( "./uploads/" ) />
-
-
-
<!--- Param the appropriate number of file fields. --->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<!--- Param file value. --->
-
<cfparam
-
name="FORM.file#intFileIndex#"
-
type="string"
-
default=""
-
/>
-
-
-
</cfloop>
-
-
-
-
-
<!--- Param upload flag. --->
-
<cftry>
-
<cfparam
-
name="FORM.submitted"
-
type="numeric"
-
default="0"
-
/>
-
-
-
<cfcatch>
-
<cfset FORM.submitted = 0 />
-
</cfcatch>
-
</cftry>
-
-
-
-
-
<!--- Set up an array to hold errors. --->
-
<cfset arrErrors = ArrayNew( 1 ) />
-
-
-
-
-
<!--- Check to see if the form has been submitted. --->
-
<cfif FORM.submitted>
-
-
-
<!---
-
Here is where we would validate the data; however,
-
in this example, there really isn't anything to
-
validate. In order to validate something, we are going
-
to require at least one file to be uploaded!
-
--->
-
-
-
-
-
<!---
-
Since we are going to require at least one file, I am
-
going to start off with an error statement. Then, I am
-
gonna let the form tell me to DELETE IT.
-
--->
-
<cfset ArrayAppend(
-
arrErrors,
-
"Please select at least one file to upload"
-
) />
-
-
-
-
-
<!--- Loop over the files looking for a valid one. --->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<cfif Len( FORM[ "file#intFileIndex#" ] )>
-
-
-
<!--- Clear the errors array. --->
-
<cfset ArrayClear( arrErrors ) />
-
-
-
<!--- Break out of loop. --->
-
<cfbreak />
-
-
-
</cfif>
-
-
-
</cfloop>
-
-
-
-
-
<!---
-
Check to see if there were any form validation
-
errors. If there are no errors, then we can continue
-
to process the form. Otherwise, we are going to skip
-
this and just let the page render again.
-
--->
-
<cfif NOT ArrayLen( arrErrors )>
-
-
-
<!---
-
Create an array to hold the list of uploaded
-
files.
-
--->
-
<cfset arrUploaded = ArrayNew( 1 ) />
-
-
-
-
-
<!---
-
Loop over the form fields and upload the files
-
that are valid (have a length).
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<!--- Check to see if file has a length. --->
-
<cfif Len( FORM[ "file#intFileIndex#" ] )>
-
-
-
<!---
-
When uploading, remember to use a CFTry /
-
CFCatch as complications might be encountered.
-
--->
-
<cftry>
-
<cffile
-
action="upload"
-
destination="#REQUEST.UploadPath#"
-
filefield="file#intFileIndex#"
-
nameconflict="makeunique"
-
/>
-
-
-
<!---
-
Store this file name in the uploaded file
-
array so we can reference it later.
-
--->
-
<cfset ArrayAppend(
-
arrUploaded,
-
(CFFILE.ServerDirectory & "\" & CFFILE.ServerFile)
-
) />
-
- <!--- Store file names in the database --->
-
<CFQUERY NAME="CreateDocumentRecord" DATASOURCE="mydb">
-
INSERT INTO Uploads (user_ID, File_Name, Date_Published)
-
VALUES (#Form.ClientID#, 'file#intFileIndex#', #Now()#)
-
</CFQUERY>
-
-
-
-
<!--- #FILE.ClientFile# #intFileIndex# File_Name, #intFileIndex#--->
-
-
-
<!--- Catch upload errors. --->
-
<cfcatch>
-
-
-
<!--- Store the error. --->
-
<cfset ArrayAppend(
-
arrErrors,
-
"There was a problem uploading file ###intFileIndex#: #CFCATCH.Message#"
-
) />
-
-
-
<!---
-
Break out of the upload loop as we
-
don't want to deal with any more
-
files than we have to.
-
--->
-
<cfbreak />
-
-
-
</cfcatch>
-
</cftry>
-
-
-
</cfif>
-
-
-
</cfloop>
-
-
-
-
-
<!--- Check to see if we have any form errors. --->
-
<cfif ArrayLen( arrErrors )>
-
-
-
-
-
<!---
-
We encountered an error somewhere in the upload
-
process. As such, we want to clean up the server
-
a bit by deleteing any files that were
-
successfully uploaded as part of this process.
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#ArrayLen( arrUploaded )#"
-
step="1">
-
-
-
<!--- Try to delete this file. --->
-
<cftry>
-
<cffile
-
action="delete"
-
file="#arrUploaded[ intFileIndex ]#"
-
/>
-
-
-
<cfcatch>
-
<!--- File could not be deleted. --->
-
</cfcatch>
-
</cftry>
-
-
-
</cfloop>
-
-
-
-
-
<cfelse>
-
-
-
-
-
<!---
-
!! SUCCESS !!
-
The files were properly uploaded and processed.
-
Here is where you might forward someone to some
-
sort of success / confirmation page.
-
--->
-
-
-
-
-
</cfif>
-
-
-
</cfif>
-
-
-
</cfif>
-
-
-
-
-
<!--- Set the content type and reset the output buffer. --->
-
<cfcontent
-
type="text/html"
-
reset="true"
-
/>
-
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html>
-
<head>
-
<title>Multiple File Uploads</title>
-
</head>
-
<body>
-
-
-
<cfoutput>
-
-
-
<h1>
-
Multiple File Upload ColdFusion Example
-
</h1>
-
-
-
-
-
<!--- Check to see if we have any errors to display. --->
-
<cfif ArrayLen( arrErrors )>
-
-
-
<p>
-
Please review the following errors:
-
</p>
-
-
-
<ul>
-
<cfloop
-
index="intError"
-
from="1"
-
to="#ArrayLen( arrErrors )#"
-
step="1">
-
-
-
<li>
-
#arrErrors[ intError ]#
-
</li>
-
-
-
</cfloop>
-
</ul>
-
-
-
</cfif>
-
-
-
-
-
<form
-
action="#CGI.script_name#"
-
method="post"
-
enctype="multipart/form-data">
-
-
-
<!--- Submission flag. --->
-
<input type="hidden" name="ClientID" value="163">
-
<input type="hidden" name="submitted" value="1" />
-
-
-
-
-
<!---
-
Loop over the number of files we are going to
-
allow for the upload.
-
--->
-
<cfloop
-
index="intFileIndex"
-
from="1"
-
to="#REQUEST.FileCount#"
-
step="1">
-
-
-
<label for="file#intFileIndex#">
-
File #intFileIndex#:
-
</label>
-
-
-
<input
-
type="file"
-
name="file#intFileIndex#"
-
id="file#intFileIndex#"
-
/>
-
-
-
<br />
-
-
-
</cfloop>
-
-
-
-
-
<input type="submit" value="Upload Files" />
-
-
-
</form>
Use CFFILE.ServerFile instead of file#intFileIndex# because file#intFileIndex# is the name of the file field in the form, not the uploaded file name.
Thank you so much acoder, it worked.
However, I am still struggling to limit the number of files to be uploaded. As you can see, at the begening of the code I pasted, you should see: - <cfset REQUEST.FileCount = 5 />
But this only displays 5 Forms from which a user can select the 5 files to upload. Once these 5 files are uploaded, he/she can keep uploading more files.
Instead, I'd like to see a message stating "you already uploaded 5 files, sorry that was your limit"
I am also working to limit the filesize, let say only 1MB file is allowed.
But, If I can get help on having a user to upload up to 5 files, I would really appreciate that.
Thanks again, you have been very helpful.
@Karim
MoroccoUSA.com
Use session variables to keep track of the number of uploads by a user. For file size, use cffile.fileSize.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: 3A Web Hosting |
last post by:
Hi
Is it possible to perform multiple file uploads via a form? It's no
problem uploading single files but I want to be able to highlight a
group of files and upload them all in one go. My...
|
by: x. zhang |
last post by:
Hi Guys,
We know that we can use <input type=file ...> to upload
one file per time to the server. My question is if there
are some way to upload multiple files per time to the
server. (Of...
|
by: Sundar |
last post by:
Hi,
I am working on ASP.Net. My requirement is that I want
to Upload Multiple Files to the Server. I need to have
ONLY ONE FILE UPLOAD CONTROL in my page. I SHOULD NOT
SUBMIT THE PAGE FOR EACH...
|
by: Jason |
last post by:
I have a potential need to upload multiple PDF legal documents. Is it
possible to attach more than one file per upload?
|
by: crowl |
last post by:
Hi there,
I am looking for a component allowing me uploading multiple files by my
asp page. I have found several components achieving this by require a
<input type="file" name="FileX"> field for...
|
by: shapper |
last post by:
Hello,
I am creating a simple CMS for a web site.
The web site will hold many documents and for that I created Documents
table which holds information on each document such as:
Title,...
|
by: dann2 |
last post by:
hello,
i try to upload in an access db two pictures at the same time. i use the adjusted sample code from persits. it looks like this:
...
'<%
' Create an instance of AspUpload object
'Set...
|
by: der_grobi |
last post by:
That is the Problem:
I have an ASP.NET Webapplicatipon where I can upload single files to
the Server.
That works fine. But now, I want to Upload multiple files. I know the
path of the files, i...
|
by: bonneylake |
last post by:
Hey Everyone,
Well this is my first time asking a question on here so please forgive me if i post
my question in the wrong section.
What i am trying to do is upload multiple files like gmail...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |