Solved

Working with files in coldfusion

Posted on 2012-03-28
5
216 Views
Last Modified: 2012-03-28
I get a daily txt file with image links (see attached txt file)textfile.txt
And I parse these links into a MySQL 5 database table.
What I want to do is download these images to a folder on my server, and rename them to match the MLS Number, like 117220-1.jpg, 117220-2.jpg, 117220-3.jpg, etc..

In looking at adobe.com I found the following bit of code that should download the image to my folder but what I need help with is when there are more than one photo per MLS Number as described above. Am I even on the right track? Any help appreciated!

<!---query my table and loop over results--->
<cfquery name="photos">
select * from listings.photos where Date Last Modified IS #Now()#
</cfquery>

<cfoutput query="photos">

<cfhttp
    method="Get"
    url="#Photo Link#"
    path="c:\myimagesfolder"
    file="#MLS Number#.jpg">
<cfoutput>
    #cfhttp.MimeType#
</cfoutput>

</cfoutput>
0
Comment
Question by:Bang-O-Matic
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
If it's not a lot of images (because cfhttp would timeout), you could use a grouped cfoutput with a counter variable.  The counter would act as a suffix for the image names.  Just increment it on each loop and reset it each time the MLS number changes.  That'll give you names like: 52188_1.jpg, 52188_2.jpg, etc...
               
* note: must order by MLS number or it won't work
* use a broader date filter than IS NOW()

<cfquery name="photos">
    SELECT  PhotoLink, MLSNumber FROM  listings.photos
    WHERE  DateLastModified > #theLastDateYouPulledImages#
    ORDER BY MLSNumber
</cfquery>

<cfoutput query="photos" group="MLSNumber">
        <!--- this is a new MLS number. reset the counter --->
        <cfset counter = 1>
        <cfoutput>
            <cfhttp method="Get" url="#PhotoLink#"
                        path="c:\myimagesfolder"
                        file="#MLSNumber#_#counter#.jpg">
             
              ... do other stuff
        </cfoutput>
</cfoutput>
0
 

Author Comment

by:Bang-O-Matic
Comment Utility
Hi _agx_, thanks for this solution! Initially I will have to download all images, approx 80,000. Then I can just run this update daily. How should I approach getting all the images the first time?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I doubt you'll be able to get all 80K in one shot.  The page will almost certainly timeout 1st, assuming the other site doesn't throttle access and block that kind of volume.

I'd probably set it up as a one time scheduled task. The task would process the images in batches of N (some reasonable number like 100 or 250). The basic process is:

- create a cfm script that queries your image table for the next N unprocessed images.
- loop through the results and for each one:
      1) download the image
      2) mark the record as processed
      lather, rinse, repeat until there are no more images

- Finally query the table to get the total unprocessed images remaining. If it's > 0, have the scheduled task run itself again. When there are no more images to process the task ends.

Though if you want to use the "grouped" output above, you'd also have to keep track of the last MLS number and counter from the previous run. Otherwise you might overwrite a few images. But you can easily pass them along along as URL parameters.
0
 

Author Comment

by:Bang-O-Matic
Comment Utility
Sounds like a plan. Thanks for your help!
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
WHERE  DateLastModified > #theLastDateYouPulledImages#

Typo correction that obviously should be on or before theLastDateYouPulledImages. ;-)

.. and I left out the increment in the pseudo code. But that's pretty self explanatory.

<cfoutput query="photos" group="MLSNumber">
        <!--- this is a new MLS number. reset the counter --->
        <cfset counter = 1>
        <cfoutput>
            <cfhttp method="Get" url="#PhotoLink#"
                        path="c:\myimagesfolder"
                        file="#MLSNumber#_#counter#.jpg">
             
              ... do other stuff
            <cfset counter = counter + 1>
        </cfoutput>
</cfoutput>
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now