Connecting Tech Pros Worldwide Forums | Help | Site Map

Best method for uploading files

Member
 
Join Date: Jan 2008
Posts: 113
#1: Nov 25 '08
Hi,

I'm trying to figure out the best method for uploading a number of files along with entering some data into a database using a component containing CRUD methods.

I have the following file structure:

index.cfm - Controller file
- display /
- create_update.cfm - Contains form for creating and updating a project
com /
- projects.cfc - Projects component containing CRUD methods for the project.

Previously i would have uploaded the files using the cffile function within one of the views or the controller file. What i'm thinking now is that perhaps the best place for this functionality is in the projects component. Is this correct?

Once i have sorted that, I am then left with the problem of how best to upload the files from the users point of view, should i do it all on one page where the user enters the data and chooses the files to upload and then presses submit, or should i break it up over a number of pages?

I guess what I'm asking is how would you do it?

Thanks,

Chromis

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Nov 25 '08

re: Best method for uploading files


Quote:

Originally Posted by chromis View Post

What i'm thinking now is that perhaps the best place for this functionality is in the projects component. Is this correct?

Seems so.

Quote:

Originally Posted by chromis

Once i have sorted that, I am then left with the problem of how best to upload the files from the users point of view, should i do it all on one page where the user enters the data and chooses the files to upload and then presses submit, or should i break it up over a number of pages?

This depends on the amount of data that the user has to enter. I would say that it would be good if the user could upload on the same page. Using JavaScript, you can give the user more flexibility by letting them choose the number of files to upload.
Member
 
Join Date: Jan 2008
Posts: 113
#3: Nov 26 '08

re: Best method for uploading files


Hey acoder, thanks for your reply.

I'll create a method in the projects.cfc for the uploading of the files then. There is one image and three files that will have to be uploaded, should i create a method which can handle any type of file or one which deals with the three files specifically?

Yes it seems to make sense to have it all on one page as there are only around 8 fields excluding the files. The thing i was worried about was the fact that it would take a while for the files to upload (one being a pdf and one being a movie), and that if there were errors in the validation and the form page was loaded with the errors, the how would i then best keep track of the files, that have been uploaded? should i store everything in the session? Again how would you go about structuring this?

When you say use javascript do you mean ajax? I had considered using an ajax function to call a cf upload function, so that the file would upload after the file was selected.

Thanks.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 26 '08

re: Best method for uploading files


Quote:

Originally Posted by chromis View Post

I'll create a method in the projects.cfc for the uploading of the files then. There is one image and three files that will have to be uploaded, should i create a method which can handle any type of file or one which deals with the three files specifically?

The first option would be better to give more flexibility.

Quote:

Originally Posted by chromis

The thing i was worried about was the fact that it would take a while for the files to upload (one being a pdf and one being a movie), and that if there were errors in the validation and the form page was loaded with the errors, the how would i then best keep track of the files, that have been uploaded?

You only need to upload when there are no errors, so validate before uploading. Uploads actually happen automatically and what the cffile upload tag does is move it to the correct location from the temporary location. If the cffile upload tag is not used, the file is deleted.

Quote:

Originally Posted by chromis

When you say use javascript do you mean ajax? I had considered using an ajax function to call a cf upload function, so that the file would upload after the file was selected.

I didn't mean Ajax. I was talking about flexibility in the number of file uploads, but since you have it fixed at 3, that's not needed.
Member
 
Join Date: Jan 2008
Posts: 113
#5: Nov 26 '08

re: Best method for uploading files


Ok, I've structured it so the following happens:

1. User submits form
2. Submitted textual data is validated using validate method of projects component
3.
a) If there are no errors with the textual data then the upload method of the projects component is called and consequently the cffile function, the upload method returns a struct containing information on the filename and whether the upload was a success or not.
b) If there are errors the user is prompted to correct them and submit again.
4. If there are no errors returned from the upload function then the project (textual data complete with the names of the uploaded files) is entered into the database.
5. A success message is displayed.

I have opted to create a separate function for each file type as I need to use different functions depending on the file type, for instance the image file requires resizing so i'm using a third party app for that cfx_imagecr3. This allows me to organise my error messages a little bit better. It would take me a while to code a function to deal with any filetype correctly, it seems the better method conceptually but perhaps this is something i could into in the future.

This is all working now save for a problem i had the other day with the arguments of the create method in the projects component which is i want to use the correct data types with my cfqueryparam calls see this post : http://bytes.com/answers/coldfusion/...n-date-row-1-a
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Nov 26 '08

re: Best method for uploading files


Nice work there! Once that data type problem is dealt with, you'll be all done.
Reply