2 Replies Latest reply on May 11, 2010 5:35 AM by PHRED-SE

    Is there an easy way to run an Ajax function from <input type=file> to test the file name in DB?

    PHRED-SE Level 1

      I've had the <input type="file">  ... <cffile ...> thing going for a few years now. 


      There is a database where the uploaded file names are stored once the files are uploaded to the server.  It sees things in terms of problems and stores uploaded file name accordingly with the ProbID prepended onto the file name; e.g., MyPicture.jpg would end up in the designated directory and databawe as P416_MyPicture.jpg.  This allows user to store pictures with the same name in different problems without a conflict.  There has been an issue with certain characters (e.g., spaces, +, #, etc.) causing problems when they are in file names so we have come up with a way using the <cffile ... rename> to replace these chars with _.  This means that MyPic+.jpg would end up being P416_MyPic_.jpg. 


           This is where the problem appears.  If someone were to upload MyPic+.jpg after someone else uploaded MyPic_.jpg in the same problem, then the + file would overwrite the _ file before the system knew they had a problem.  And there is now way to restore the original file without going to the system backup and doing so – which is a whole other story … especially, if the person doesn’t tell anyone.


           The logical solution would be to be able to test the new final file name (the name after making the substitutions mentioned above) against the existing files in the database before you went from the page where the <input type=file> control to the associated _action.cfm page where the <cffile> object is located.  Given that this seems to be the province of Ajax, this would seem like a natural use of the really interesting technology.  I have can determine the file name from the onChange action on the <input type=File> so that this would be the place for Ajax to come to the rescue by looking up the final file name and then letting the user know whether the resultant name is a unique within the database or not.  If so, I'll just enable the Add button which sets right next to the <input > control and let them upload it.  If not, I'll put up an error message telling them that this file already exists in the system.


           This would be the perfect solution.  It would let me do some Ajax stuff like I've wanted to do for the past year, but never have had the time to do because this is the project that won't go away.  This is, in fact, the last thing of any consequence that remains to do on this 2-1/2 year nightmare – be careful what you wish for.  If I can get this done this week, then maybe I can finally take a weekend off … maybe it will finally come to an end.


           Which finally leads to my question:  I'm looking for some tips on how to get this thing going since I can barely spell Ajax.  I've got a book and looked at some stuff online about CF and Ajax, but a good example or two or three would be worth a day's worth of poking around on the web and in my books.


           Thanks in advance for any suggestions, ideas, help, whatever.




        • 1. Re: Is there an easy way to run an Ajax function from <input type=file> to test the file name in DB?
          Adam Cameron. Level 5

          I'm not sure there's a need for AJAX here, or that it's the best approach.  You want to ping a request back to the server saying "you're about to receive this file, better make sure there's no file name conflicts, and..." then what?  What's the server's response to the client going to be?  You're not expecting the client side to be told to change the file name, are you?  I don't think there is a client/server interaction here.  Am I missing some part of the process here?


          All I'd do is to upload the file to a staging area, using the MAKEUNIQUE attribute of <cffile> to make sure there are no filename conflicts, then work out what the new file name needs to be for the file respository it's going to end up in, then move it to the correct dir with the correct file name and make the matching DB insert/update/whatever.  And in the ensuing response to the client, advise if the file name needed to be changed (should they need to know that).




          • 2. Re: Is there an easy way to run an Ajax function from <input type=file> to test the file name in DB?
            PHRED-SE Level 1



                 Thank you for your suggestion, but, after spending the night working with Ben Forta's CF8, vol 2, Chap 34, working with <cfajaxproxy>, I was able to do exactly what I wanted and it appears, after some testing, to work exactly as I had envisoned it should. 


                 I am now calling a JavaScript function (testFileName) from the onChage event on the <input type="file"> or Browse button, which calls my proxy.cfc that contains the server side of the equation.  This funciton testFileName (I've run out of cleaver function/file names).  The query contained therein hits the database to see if the passed in parameters can pull up an existing file.  If they do, the particulars (file name, data attahced, etc.) are returned to the JavaScipt procedure that puts up the error notice.  The user can then chose to either overwrite the file or quit.  Quiting leaves the your on the Attachment PopUp where he/she started with nothing being uploaded. 


                 Thanks again for your suggestion.  I appreciate your taking the time to do so.