下面是以前一個功能的實作程式碼,現在因為要整合進MVC裡面,所以想使用MVC改寫一下,發現知識有些匱乏,實在不知道在MVC裡面應該怎麼讀取二進位流,然後在視圖中展示。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim QstrOffSet As String = Request.QueryString("offs")
If Not String.IsNullOrEmpty(QstrOffSet) Then
Dim FileName As String = Server.MapPath("App_Data\img.dat")
Dim openDataStream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim openDataBR As New BinaryReader(openDataStream)
Try
Dim OffSet As UInt32 = Convert.ToUInt32(QstrOffSet)
openDataStream.Seek(OffSet, SeekOrigin.Begin)
Dim Length As UInt32 = openDataBR.ReadUInt32()
If Length < 33000 Then
Response.ContentType = "application/x-MS-bmp"
Dim myByte() As Byte = openDataBR.ReadBytes(Length)
Response.BinaryWrite(myByte)
End If
Catch ex As Exception
Finally
openDataBR.Close()
openDataStream.Close()
openDataStream.Dispose()
End Try
End If
End If
End Sub
像
Response.ContentType = "application/x-MS-bmp"
Response.BinaryWrite(myByte)
這兩段程式碼應該是關鍵問題所在,在MVC中我還不知道有什麼API可以去處理這樣的問題,希望能得到大家的幫助。
http://stackoverflow.com/questions/7163448/mvc-controller-using-response-stream