Flickr logo
More of Luca Mauri's photos
blank

Section's icon A snippet
Check and download images
Codename: webimages

In the following code you can see how to make a conditional download of images.
Let's assume you have a list of image files in a table (from a database, for instance, but any other source will do as well) and the folder on the remote website where the images are.

Those data are set in the very beginning of the file, the only external object you have to instantiate before calling this function is the dataset called dsImgs. Once done, the function will look for all the images in the location and will check if the image already exist on the disk. If this is the case, a check is done on the Last-Modified date and the file is downloaded if the current version is older only.

Separatore

Following the full code of the snippet
You can copy and paste it from here, otherwise you can download it in .snippet file format by clicking here.

                Imports System.Net
Imports System.IO

Module modImages
    Function getimages() As Boolean
        Dim prefixFull As String = "http://www.yoursite.com/images/"
        Dim prefixShort As String = "http://www.yoursite.com"
        Dim basePath As String = "c:\your path here"

        Dim imgClient As WebClient
        Dim imgRequest As WebRequest
        Dim imgResp As WebResponse

        Dim lastMod As Date
        Dim imgInfo As FileInfo

        Dim imgPath As String
        Dim destPath As String
        Dim filePath As String

        Dim folder As String

        Try
            For Each immagine As DataRow In dsImgs.Tables(0).rows
                Select Case immagine("file").ToString.Substring(0, 1)
                    Case "/"
                        imgPath = prefixShort & immagine("file")
                        filePath = immagine("file").ToString.Substring(1).Replace("/", "\")
                    Case Else
                        imgPath = prefixFull & immagine("file")
                        filePath = immagine("file").ToString.Replace("/", "\")
                End Select
                destPath = Path.Combine(basePath, filePath)

                imgClient = New System.Net.WebClient

                If File.Exists(destPath) Then
                    Try
                        imgRequest = WebRequest.Create(imgPath.TrimStart)
                        imgResp = imgRequest.GetResponse
                        lastMod = DateTime.Parse(imgResp.Headers("Last-Modified"))
                        imgInfo = New FileInfo(destPath)
                        If DateTime.Compare(imgInfo.CreationTime, lastMod) > 0 Then
                            Console.WriteLine("SKIP - " & destPath)
                            Continue For
                        End If
                    Catch ex As Exception
                        Console.WriteLine("ERROR - " & imgPath)
                    Finally
                        If imgResp IsNot Nothing Then
                            imgResp.Close()
                        End If
                    End Try
                End If

                folder = destPath.Substring(0, destPath.LastIndexOf("\"))
                If Not Directory.Exists(folder) Then
                    Directory.CreateDirectory(folder)
                End If

                Try
                    imgClient.DownloadFile(imgPath.TrimStart, destPath)
                    Console.WriteLine(destPath)
                Catch ex As Exception
                    Console.WriteLine("ERROR 2 - " & imgPath)
                End Try

                imgClient.Dispose()
            Next

            Console.WriteLine("Completed")
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Return False
        End Try
    End Function
End Module                
            

Go back to snippet page.

Gear B/N Risultato del processo OK
blank

Ajax-enabled news - Beta

Ajax News temporaneamente sospese in attesa della nuova versione.

Tag Cloud - Beta

AddThis Bookmark

Il pulsante di AddThis permette di aggiungere un segnalibro per questa pagina nei più diffusi siti internet.

Licenza

Creative Commons License
Il contenuto di questo sito internet, se non diversamente specificato, è pubblicato sotto una Licenza Creative Commons.

The content of this website is published under a Creative Commons License, unless otherwise stated.

Site Meter

ClustrMaps

Locations of visitors to this page

Elenco Feed

Questo file OPML contiene l'elenco aggiornato di tutti i feed RSS disponibili sul sito.

OPML rect

amung.us

hit counter

Random Quote

«- Your compassion is a weakness your enemies will not share.
- That's why it's so important. It separates us from them. »

Henri Ducard, Bruce Wayne
Batman Begins

Leggi tutte le citazioni

Random Fact

Il satellite artificiale Wilkinson Microwave Anisotropy Probe ha contribuito in maniera sostanziale alla risoluzione di molti problema della cosmologia moderna.
Di seguito riporto una breve panoramica delle caratteristiche dell'universo che sono state definite grazie alle sue scoperte:

  • L'età dell'universo è 13,7 miliardi di anni, con una tolleranza ±0,2
  • L'universo è composto da
    • 4% di materia barionica (ordinaria)
    • 22% di un tipo ancora sconosciuto di Materia Oscura che non emette o assorbe luce
    • 74% di una Energia Oscura del tutto sconosciuta che agisce accelerando l'espansione
  • La costante di Hubble vale 70 (km/s)/Mpc, con tolleranza +2,4/-3,2
  • La geometria dell'Universo appare piatta con Ω=1,02 e tolleranza ±0,2
  • La teoria dell'inflazione nella sua versione basilare è confermata dalle osservazioni

Il WMAP è probabilmente il singolo strumento più proficuo che sia mai stato al servizio della Cosmologia.

Kaku (2005)

Leggi tutte le curiosità

aNobii Books

Sorting Hat

i'm in ravenclaw!
be sorted @ nimbo.net

LM CubeCopyright © 2000 - 2010 Luca Mauri. All rights reserved
Note sui contenuti del sito.


Generated by Luca Mauri's xCMS xCMS | ASP.net 2.0 powered Powered by ASP.net 2.0

Technology Blogs - BlogCatalog Blog Directory


Standard compliant: Valid Sitemaps | W3C CSS Valid | W3C XHTML Valid | RSS 2.0 Valid | OPML checked by validator.opml.org. | AJAX Powered


This page is generated by Luca Mauri Luca Mauri's xCMS
eXperimental Content Management System version 1.5.0.11320
Current date and time on server Tue, 07 Sep 2010 15:33:29 GMT
Took 0,3846228 seconds to be created