如何使用http处理程序将图像路径返回给客户端

时间:2014-11-06 11:28:57

标签: asp.net httphandler

我在我的项目中使用Http Handler,我希望当客户端向我发送一个简单的请求时,我想在服务器上读取我的文件并返回客户端我的图像文件URL,然后客户端读取此URL并下载图像。

1 个答案:

答案 0 :(得分:0)

这是如何检索图像缩略图的示例,但您可以修改它以检索整个图片,而不是缩略图。

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim ID As String = HttpContext.Current.Request.QueryString("HotelCode")
        Dim con As String = ConfigurationManager.ConnectionStrings("hotelbedsConnectionString").ConnectionString
        Dim logCon As New SqlConnection(con)
        Dim cmd As New SqlCommand("Select ImagePath From HOTEL_IMAGES WHERE HotelCode = @HotelCode AND Order_='1'", logCon)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add("HotelCode", SqlDbType.Int, 0).Value = ID
        logCon.Open()
        Dim MyData() As Byte
        Dim wc = New WebClient
        Dim path As String = cmd.ExecuteScalar()
        If path Is Nothing Then
            MyData = wc.DownloadData("http://www.blogasp.net/Images/nopic.jpg")
        Else
            MyData = wc.DownloadData("http://www.hotelbeds.com/giata/" & path)
        End If
        Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(MyData)
        Dim img As System.Drawing.Image = Image.FromStream(ms)
        Dim w As Integer = 100 '*** Fix Width ***' 
        Dim ratio As Double = img.Height / img.Width
        Dim h As Integer = ratio * w
        Dim thumbnail As Image = New Bitmap(w, h)
        Dim graphics__2 As Graphics = Graphics.FromImage(thumbnail)
        graphics__2.InterpolationMode = InterpolationMode.HighQualityBicubic
        graphics__2.SmoothingMode = SmoothingMode.HighQuality
        graphics__2.PixelOffsetMode = PixelOffsetMode.HighQuality
        graphics__2.CompositingQuality = CompositingQuality.HighQuality
        graphics__2.DrawImage(img, 0, 0, w, h)
        Dim info As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
        Dim encoderParameters As EncoderParameters
        encoderParameters = New EncoderParameters(1)
        encoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 100L)
        thumbnail.Save(HttpContext.Current.Response.OutputStream, info(1), encoderParameters)
        logCon.Close()
        Finalize()
    End Sub