开发者

ASP.NET MVC FileContentResult SLOW

We are using it to return a file for an export. When we run this export on a lot of records, it takes close to 10 minutes to run. Here is a code snippet of the code that actually calls the File() method and returns the result.

Public Function Export(ByVal ID As Integer) As FileContentResult
  Dim str As String = String.Empty
  Dim data() As Byte
  Dim r As New ExportResult
  Dim Test As New TestConnection(WebUtil.UserToken)

  'This line is important coz IE download was prevented without this.
  ControllerContext.HttpContext.Response.ClearHeaders()

  r = Test.ExportFile(ID)
  data = Encoding.ASCII.GetBytes(r.ResponseString)

  Return File(data, "text/plain", r.DefaultFileName)

End Function

The actual ExportFile method takes in an ID, calls another method, which gets a bunch of records from the database, performs a bunch of calculations on each row, and then creates a StringBuilder, and for each row populates the StringBuilder and then pops it into a List(Of String), after doing a .ToString(). Then this method returns the List(Of String) to the ExportFile method, and this method creates another StringBuilder, appends all the Strings from that list, converts it to one large String, and sets it to the ResponseString property of the result ('r' in the code above).

So that's how it works. Is there anyway to speed this process up, like, a lot?

-Scott

Edit: More Code

Public Function ExportFile(ByVal ID As Integer) As ExportResult
            Dim result As New ExportResult
            Dim s As New StringBuilder

            'Get all Records
            Dim dt As New DataTable
            Using dal As New SQL
                dal.Parameters.AddWithValue("@ID", ID)
                dal.Execute("[dbo].[uspGet]", dt)
                dal.Parameters.Clear()
            End Using

            Dim dataobj As New DataObj(dt, ID)

            'Create FileName
            If d开发者_开发问答t.Rows.Count > 0 Then
                Dim StartDate As DateTime = DateTime.Parse(dt.Rows(0).Item("StartDate"))
                Dim EndDate As DateTime = DateTime.Parse(dt.Rows(0).Item("EndDate"))
                result.DefaultFileName = String.Format("HMDA_{0}_{1}.dat", String.Format("{0:MMyyyy}", StartDate), String.Format("{0:MMyyyy}", EndDate))
            End If

            'Add Title Line
            s.AppendLine(dataobj.CreateTitleLine())

            'Add all Record Lines
            Dim records As List(Of String) = dataobj.CreateRecordLines()
            Dim last As Integer = records.Count - 1
            For i = 0 To last
                If i = last Then
                    s.Append(records(i))
                Else
                    s.AppendLine(records(i))
                End If
            Next

            result.ResponseString = s.ToString

            Return result
End Function


How performant is your SQL? I'd start checking there for your problems.

Then consider how big your file is. The data still needs to be downloaded at the client.


Avoid extra copies? Maybe you can avoid the conversion to bytes and / or the creation if the file. Just write the string to the response without this extra work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜