2010-05-30 46 views
0

我想实现流文件的ihttphandeler。文件可能是微小的缩略图或巨大的电影
二进制文件[R存储在SQL Server
我看了网上很多代码,但事情没有意义
是不是流应该读一块数据块并将其移动在线?
大部分代码似乎首先从mssql读取整个字段到内存,然后使用流式输出写入
从字节(或缓冲区块)实际直接从磁盘直接传输到http字节不是更高效吗? )
继承人到目前为止我的代码,但无法弄清楚的SQLReader的模式的正确组合和流对象,并使用 端子.net IHTTPHandler流SQL二进制数据

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,文档...)在查询的第二场

谢谢大家非常

编辑:

我设法找到一些更多的代码,现在该页面间歇性地显示。有时它带来的PDF文件,有时它不
我不能理解这里的模式
我认为,主要问题是当请求是从不同的页面,我点击“显示在新标签”,然后它从来没有工作。当我做“在新窗口中展示”时,它大多可用,但并非总是如此。
btw。代码总是运行。从来没有打破或错误或类似的东西。它在每次请求时都像一个好孩子一样从头到尾运行
有时在很长时间后IE会给我一条消息(来自新标签)“Adobe/Acrobat Reader有问题,请退出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 

非常感谢你

回答

0

的intermittance已经停止。不知道为什么。

但现在它的工作正常