2014-04-16 60 views
3

我有一個包含圖像URL的數據庫。我有一個存儲過程的帽子GET的url的(SP_GET_Image)我想執行存儲過程,然後爲每個URL的圖像。從數據庫中的url中獲取圖像

我想在本地的實際圖像不是網址。

然後對於每個圖像我想在本地保存它們。我有這個代碼,但想知道如何將每個圖像保存在數據行中。

我已經開始使用代碼。

Dim CMD as new sqlCommand("StoredProcedureName") 
CMD.parameters("@Parameter1", sqlDBType.Int).value = Param_1_value 

Dim connection As New SqlConnection(connectionString) 
CMD.Connection = connection 
CMD.CommandType = CommandType.StoredProcedure 

Dim adapter As New SqlDataAdapter(CMD) 
adapter.SelectCommand.CommandTimeout = 300 

'Fill the dataset' 
Dim DS as DataSet  
adapter.Fill(ds) 
connection.Close() 

'Now, read through your data:' 
For Each DR as DataRow in DS.Tables(0).rows 
'<-- Im not sure here how to GET EACH images locally saved. 
Next 

c#或vb幫助很好。

的URL看起來是這樣的:

http://img.myCompany.net/p/1483/278227_20094171232290.jpg

+0

你能提供由SP返回的那些'DataRow'中的結構和樣本值嗎?這將是一個完整的網址,如「http:// ww.google.com/logo.png」? –

+0

@Mathew編輯原始文章 – user3458266

+1

這是你的需求? 「你的數據庫中有一些URL,你必須將它們保存到你的項目文件夾中,是嗎?」 – naveen

回答

1

可以以下載和存儲文件的本地計算機或遠程服務器提供用戶名和密碼(如果需要)上使用My.Computer.Network.DownloadFile。當你需要指定下載時的文件名,你可以從網址與SubString(URL.LastIndexOf("/") + 1)

For Each DR as DataRow in DS.Tables(0).Rows 
     Dim URL as String = DR("Your_URL_Column_Name").ToString() 
     Dim Destination as String = "\\SERVERNAME\FolderName\" 
     My.Computer.Network.DownloadFile(URL, Destination & SubString(URL.LastIndexOf("/") + 1), "name", "password") 
Next 
+0

我假設「Your_URL_Column_Name」它是我的存儲過程返回的url的列名稱? – user3458266

+0

@ user3458266準確地 – equisde

0

提取,則此功能會幫你下載一個圖像列表到指定的本地路徑

public void DownloadFiles(IEnumerable<string> urls, string path) 
    { 
     if (!System.IO.Directory.Exists(path)) 
      System.IO.Directory.CreateDirectory(path); 

     System.Threading.Tasks.Parallel.ForEach(urls, url => 
     { 
      using (var downloader = new WebClient()) 
      { 
       var filePath = System.IO.Path.Combine(path, System.IO.Path.GetFileName(url)); 
       downloader.DownloadFile(url,filePath); 
      } 
     }); 
    } 

你可以用它與此類似:

var urlList= DS.Tables[0].Rows 
         .Cast<DataRow>() 
         .Select(x => x["YourColumnNameOfUrl"].ToString()); 
DownloadFiles(urlList,"C:\Directory\Of\Ur\Choice\"); 
+0

如果我想下載到服務器,我可以更改url,filepath? – user3458266

+0

@ user3458266是的,您可以根據需要調整參數。在Web應用程序等中從服務器運行它。 –

0

這裏是一個小工具功能,以幫助您與您的任務

Function SaveRemoteImage(remoteImageUrl As String) As Integer 
    Try 
     Dim request = WebRequest.Create(remoteImageUrl) 
     Dim folderName = Server.MapPath("~/VB/Images/") 
     Using response As WebResponse = request.GetResponse() 
      Using stream As Stream = response.GetResponseStream() 
       Dim imageExtension = String.Empty 
       Select Case response.ContentType.ToLower 
        Case "image/bmp", 
         "image/x-bmp", 
         "image/x-ms-bmp" 
         imageExtension = ".bmp" 

        Case "image/jpeg" 
         imageExtension = ".jpeg" 

        Case "image/gif" 
         imageExtension = ".gif" 

        Case "image/png" 
         imageExtension = ".png" 

        Case Else 
         imageExtension = ".png" 
       End Select 
       'renaming image name as GUID to avoid conflicts 
       Dim imageName = Guid.NewGuid().ToString() 
       ' Download the file 
       Dim destinationPath = String.Concat(
        folderName, 
        imageName, 
        imageExtension) 
       Using tempFile = File.OpenWrite(destinationPath) 
        ' Remark: if the file is very big read it in chunks 
        ' to avoid loading it into memory 
        Dim buffer = New Byte(response.ContentLength - 1) {} 
        stream.Read(buffer, 0, buffer.Length) 
        tempFile.Write(buffer, 0, buffer.Length) 
       End Using 
      End Using 
     End Using 
     Return True 
    Catch ex As Exception 
     Return False 
    End Try 
End Function 

我沒有使用WebClient方法,因爲我們需要正確的Image Content-Type來獲取本地文件擴展名。

現在,讓所有ImageUrls從DataTableIEnumerable(Of String)並調用它像這樣

Dim images = table.AsEnumerable(). 
    Select(Function(row) row.Field(Of String)("ImageUrl")) 
For Each remoteImage In images 
    SaveRemoteImage(remoteImage) 
Next 

如果你想要一些並行編程的魔法,更換For Each這樣。

System.Threading.Tasks.Parallel.ForEach(images, 
    Function(remoteImage) SaveRemoteImage(remoteImage))