搜尋

首頁  >  問答  >  主體

asp.net-mvc - 在asp.net MVC5中如何實作web form中的二進位流檔案輸出?

下面是以前一個功能的實作程式碼,現在因為要整合進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可以去處理這樣的問題,希望能得到大家的幫助。

迷茫迷茫2751 天前391

全部回覆(1)我來回復

  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:08:09

    http://stackoverflow.com/questions/7163448/mvc-controller-using-response-stream

    回覆
    0
  • 取消回覆