.net IHTTPHandler流式SQL二进制数据

时间:2010-05-30 11:48:07

标签: .net ihttphandler

我正在尝试为流文件实现ihttphandeler。文件可能是微小的缩略图或巨大的电影 二进制文件r存储在sql server中 我在网上看了很多代码,但有些东西没有意义 是不是流媒体应该逐个读取数据并将其移动到线上? 大多数代码似乎首先从mssql到内存读取整个字段,然后使用流式输出写入
实际上从磁盘直接流到http的字节(或缓冲的块?)是不是更有效率 继承了我的代码到目前为止,但无法找出sqlreader模式与流对象和书写系统的正确组合

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
     context.Response.BufferOutput = False
    Dim FileField=safeparam(context.Request.QueryString("FileField"))
     Dim FileTable=safeparam(context.Request.QueryString("FileTable"))
     Dim KeyField=safeparam(context.Request.QueryString("KeyField"))
     Dim FileKey=safeparam(context.Request.QueryString("FileKey"))                 
    Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("Main").ConnectionString)
        Using command As New SqlCommand("SELECT " & FileField & "Bytes," & FileField & "Type FROM " & FileTable & " WHERE " & KeyField & "=" & FileKey, connection)
            command.CommandType = Data.CommandType.Text

结束使用 结束使用 结束子

请注意,此sql命令还会在查询的第二个字段中返回文件扩展名(pdf,jpg,doc ...)

非常感谢你

编辑:

我设法找到了更多代码,现在页面间歇性地出现了。有时它带来pdf文件,有时它不会 我无法理解这里的模式 我认为主要问题是当请求来自不同的页面并且我点击“在新标签中显示”然后它从未起作用。当我做“在新窗口中显示”时,它主要起作用但并非总是如此 顺便说一句。代码总是运行。从不打破或错误或类似的东西。每次请求时,它都像一个好孩子一样从头到尾运行 有时IE很长一段时间后,给我一条消息(来自新标签)“Adobe / Acrobat阅读器有问题。请退出Adobe Acrobat / Reader再试一次。”
问题是什么?
继承我当前的代码

Shared Sub ProccessMedia(ByVal context As HttpContext)
    If CurPerson Is Nothing OrElse Not CurPerson.PersonExts.FirstOrDefault.LetAllFiles Then Exit Sub
    context.Response.BufferOutput = False
    Dim FileField = SafeParam(context.Request.QueryString("FileField"))
    Dim FileTable = SafeParam(context.Request.QueryString("FileTable"))
    Dim KeyField = SafeParam(context.Request.QueryString("KeyField"))
    Dim FileKey = SafeParam(context.Request.QueryString("FileKey"))
    Dim oSqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Main").ConnectionString)
    Dim oSqlCommand = New SqlCommand("SELECT " & FileField & "Type," & FileField & "Bytes FROM " & FileTable & " WHERE " & KeyField & "=" & FileKey, oSqlConnection)
    oSqlConnection.Open()
    Dim oSqlDataReader = oSqlCommand.ExecuteReader(CommandBehavior.SequentialAccess)
    If oSqlDataReader.Read() Then
        context.Response.ContentType = GetMIMEType(oSqlDataReader.GetString(0))
        Dim bufferSize = 8040
        Dim chunk = New Byte(bufferSize - 1) {}
        Dim retCount As Long
        Dim startIndex As Long = 0
        retCount = oSqlDataReader.GetBytes(1, startIndex, chunk, 0, bufferSize)
        While retCount = bufferSize
            context.Response.BinaryWrite(chunk)
            startIndex += bufferSize
            retCount = oSqlDataReader.GetBytes(1, startIndex, chunk, 0, bufferSize)
        End While
        oSqlDataReader.Close()
        oSqlConnection.Close()
        Dim actualChunk = New Byte(retCount - 2) {}
        Buffer.BlockCopy(chunk, 0, actualChunk, 0, CInt(retCount) - 1)
        context.Response.BinaryWrite(actualChunk)
    End If
End Sub

非常感谢

1 个答案:

答案 0 :(得分:0)

间歇性已经停止。不知道为什么。

但现在工作正常