我想實現流文件的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
非常感謝你