Created
August 15, 2008 14:22
-
-
Save dieseltravis/5572 to your computer and use it in GitHub Desktop.
saving an image from the web to a local path and returning a stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Shared Function GetExternalImage(ByVal url As String) As IO.Stream | |
Dim parts As String() = url.Split("."c) | |
Dim ext As String = parts(parts.Length - 1) | |
Dim path As String = HttpContext.Current.Server.MapPath("~/App_Data/rss/photogallery/") | |
If Not Directory.Exists(path) Then | |
Directory.CreateDirectory(path) | |
End If | |
Dim stream As IO.Stream = Nothing | |
If File.Exists(cacheFilePath) Then | |
stream = File.OpenRead(cacheFilePath) | |
Else | |
Try | |
Dim httpWebRequest As HttpWebRequest = WebAccess.GetWebRequest(url, True, True) | |
Dim httpWebResponse As HttpWebResponse = WebAccess.GetWebResponse(httpWebRequest) | |
If httpWebResponse IsNot Nothing AndAlso httpWebResponse.StatusCode <> HttpStatusCode.NotFound Then | |
stream = httpWebResponse.GetResponseStream() | |
Dim r As New BinaryReader(stream) | |
File.WriteAllBytes(cacheFilePath, r.ReadBytes(stream.Length)) | |
stream = File.OpenRead(cacheFilePath) | |
End If | |
Catch ex As Exception | |
EventRecorder.WriteToEventViewer(ex.Message) | |
End Try | |
End If | |
Return stream | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment