来自页面请求的asp.net vb流文件

时间:2017-07-14 18:45:25

标签: asp.net vb.net httprequest httpresponse

我正在尝试将动态创建的csv文件作为对页面请求的响应。我有代码来创建一个可以由客户端下载的文件,但这不是解决方案。我找到了可以流式传输文件的代码,但文件似乎必须存在于文件系统中,如果我能帮助它,我宁愿不必使用临时文件。

这就是我所拥有的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim csvTable As DataTable = pointsTable_Select_By_playerName_for_CSV()
    Dim sb = New StringBuilder
    For Each dcol As DataColumn In csvTable.Columns
        sb.Append(dcol.ColumnName & ",")
    Next
    sb.Append(ControlChars.NewLine)
    For Each drow As DataRow In csvTable.Rows
        For i As Integer = 0 To csvTable.Columns.Count - 1
            sb.Append(drow.Item(i).ToString & ",")
        Next
        sb.Append(ControlChars.NewLine)
    Next

    Response.Clear()
    Response.ContentType = "text/csv"
    Response.AppendHeader("Content-Disposition",
            String.Format("attachment; filename={0}.csv", DateTime.Now))
    Response.Write(sb.ToString)
    Context.Response.End()
End Sub

我发现流式传输文件的代码:

        'Create a stream for the file
    Dim stream As Stream = Nothing

    'This controls how many bytes to read at a time and send to the client
    Dim bytesToRead As Integer = 10000

    ' Buffer to read bytes in chunk size specified above
    Dim buffer As Byte() = New [Byte](bytesToRead - 1) {}

    ' The number of bytes read
    Try
        'Create a WebRequest to get the file
        Dim fileReq As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)

        'Create a response for this request
        Dim fileResp As HttpWebResponse = DirectCast(fileReq.GetResponse(), HttpWebResponse)

        If fileReq.ContentLength > 0 Then
            fileResp.ContentLength = fileReq.ContentLength
        End If

        'Get the Stream returned from the response
        stream = fileResp.GetResponseStream()

        ' prepare the response to the client. resp is the client Response
        Dim resp = HttpContext.Current.Response

        'Indicate the type of data being sent
        resp.ContentType = "application/octet-stream"

        'Name the file 
        resp.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.csv", DateTime.Now))
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString())

        Dim length As Integer
        Do
            ' Verify that the client is connected.
            If resp.IsClientConnected Then
                ' Read data into the buffer.
                length = stream.Read(buffer, 0, bytesToRead)

                ' and write it out to the response's output stream
                resp.OutputStream.Write(buffer, 0, length)

                ' Flush the data
                resp.Flush()

                'Clear the buffer
                buffer = New [Byte](bytesToRead - 1) {}
            Else
                ' cancel the download if client has disconnected
                length = -1
            End If
            'Repeat until no data is read
        Loop While length > 0
    Finally
        If stream IsNot Nothing Then
            'Close the input stream
            stream.Close()
        End If
    End Try

问题是这段代码需要指向一个文件,有没有办法可以使用流将字符串从sb移动到httpresponse?

1 个答案:

答案 0 :(得分:0)

您可以使用Action Not Found for request '/count'

此外,如果您的数据可能包含Response.Write(),您应该通过放置在,之类的文本限定符中来转义它。

除非从中删除特殊字符,否则您也无法使用日期时间作为文件名。

"