I've got a simple web-page that generates a CSV file that I want the user to be able to download once its creation is complete.
Here's a summary of my situation:
Here's the code I've got working to send the .zip containing PDF files:
Protected Sub SaveReportsButton_Click(sender As Object, e As EventArgs) Handles SaveReportsButton.Click
'' The .zip file is created elsewhere
Dim fullPath As String = Server.MapPath("~/Reports/" & Session.SessionID & ".zip")
Response.ContentType = "APPLICATION/OCTET-STREAM"
Dim fileToDownload As New FileInfo(fullPath)
Dim disHeader As String = "Attachment; Filename=""Reports.zip"""
Response.AppendHeader("Content-Disposition", disHeader)
Response.Flush()
Response.WriteFile(fileToDownload.FullName)
End Sub
Here's the code I'm trying to get working to transmit the CSV file:
Protected Sub saveButton_Click(sender As Object, e As System.EventArgs) Handles SaveOutput.Click
Dim list As List(Of String) = {"000000000", "000000000", "000000000"}.ToList()
Dim sb As New Text.StringBuilder
For i As Integer = 0 To list.Count - 1
Dim str As String = list(i)
sb.Append(str)
If i < list.Count - 1 Then
sb.Append(",")
End If
Next
Dim dirPath As String = Server.MapPath("~/Output/" & Session.SessionID)
IO.Directory.CreateDirectory(dirPath)
Dim filePath As String = Server.MapPath("~/Output/" & Session.SessionID & "/Output.csv")
IO.File.WriteAllText(filePath, sb.ToString())
Response.ContentType = "text/csv"
Dim disHeader As String = "Attachment; Filename=""Output.csv"""
Response.AppendHeader("Content-Disposition", disHeader)
Response.Flush()
Response.WriteFile(filePath)
End Sub
Like I said, I wish to limit the files created and left on the server by this page, so I'd like to get rid of the CSV file once I'm done with it.
Any help?
Thanks!!!
try this with Response.Write and Response.End
Protected Sub saveButton_Click(sender As Object, e As System.EventArgs) Handles SaveOutput.Click
Dim list As List(Of String) = {"000000000", "000000000", "000000000"}.ToList()
Dim sb As New Text.StringBuilder
For i As Integer = 0 To list.Count - 1
Dim str As String = list(i)
sb.Append(str)
If i < list.Count - 1 Then
sb.Append(",")
End If
Next
Response.Clear()
Response.ContentType = "text/csv"
Dim disHeader As String = "Attachment; Filename=""Output.csv"""
Response.AppendHeader("Content-Disposition", disHeader)
Response.Flush()
Response.Write(sb.ToString())
Response.End()
End Sub