0
我想創建一個csv文件的基礎上,當用戶點擊一個按鈕的gridview的內容。但是,當我點擊按鈕時,我不斷收到Connection被服務器重置。提前致謝。ASP.NET生成文件以下載連接重置
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Try
Dim out As IO.TextWriter = Response.Output
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.csv")
Response.Charset = ""
Response.ContentType = "application/text"
gvFiles.AllowPaging = False
gvFiles.DataBind()
Me.EnableViewState = False
'Bind DataTable to GridView
'Search() method returns a DataTable of search results
'gvFiles.DataSource = Search()
'gvFiles.DataBind()
'String builder class to add row data
Dim sb As New StringBuilder()
For k As Integer = 0 To gvFiles.Columns.Count - 1
'add separator
sb.Append(gvFiles.Columns(k).HeaderText & ",")
Next
'append(New line)
sb.Append(vbCrLf)
'Get Rows
For i As Integer = 0 To gvFiles.Rows.Count - 1
'Get columns
For k As Integer = 0 To gvFiles.Columns.Count - 1
'add separator
sb.Append(gvFiles.Rows(i).Cells(k).Text & ",")
Next
'append new line
sb.Append(vbCrLf)
Next
out.Write(sb.ToString())
out.Flush()
'out.Close()
Response.End()
Catch ex As Exception
Response.Write("Unable to generate File" & ex.ToString())
End Try
End Sub