List Info

Thread: RE: Downloading a CSV file




RE: Downloading a CSV file
user name
2007-03-22 12:20:12
So filename is the name I want to give to the file being downloaded?
 
Response.AppendHeader("Content-Disposition", "attachment;filename=" & filename)

 
On 3/22/07, Christopher Frazier < chris.fraziergmail.com">chris.fraziergmail.com> wrote:

Just stream it, yo! Instead of sw.Write'ing to the filesystem, do it to the response stream. You're writing to a stream already, this is just changing where it gets delivered.

 

 &nbsp; &nbsp;  Private Sub CreateXLSFile(ByVal FilePath As String, ByVal TextToAdd As String)

 &nbsp;   ; &nbsp;  Try
   ; &nbsp; &nbsp; &nbsp; &nbsp; Dim sw As StreamWriter = New StreamWriter(Response.OutputStream)
 &nbsp; &nbsp; &nbsp;   ; &nbsp; sw.Write(TextToAdd)
&nbsp;   ; &nbsp; &nbsp; &nbsp;  sw.Close()
 &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As Exception

&nbsp; &nbsp; &nbsp;   ; End Try

&nbsp;   ;  End Sub

You can also add a header to make the browser handle the file with whatever name you choose using Response.AppendHeader("Content-Disposition", "attachment;filename=" & filename) This will open the default viewing program on the user's machine. If you wanted it to just show in the browser, you would switch "attachment" with "inline".

 

hth,

--
-Christopher
| AspInsider
http://chrisfrazier.net/blog

 

(__/)

(='.&#39;=) This is Bunny. Copy and paste bunny into your

(")_(") signature to help him gain world domination.

 

From: David Douglass [mailto:gmail.com" target="_blank">dpdouggmail.com ]
Sent: Thursday, March 22, 2007 11:09 AM
To: aspadvice.com" target="_blank">aspnetaspadvice.com
Subject: [aspnet] Downloading a CSV file

 

I have functionality (see code below) that allows the user to download a certain set of data from the database in the form of a CSV file.

 

It writes the file to the server first and then the user is able to download it and save or open it. The problem is that I have never been able to delete the file on the server after the process is finished. I have tried ;deleting it in code and, evidently&nbsp;the server just doesn't permit deleting through code. I also tried using a SQL Server job to delete all CSV files on a regular basis, but the server won't permit that either. What I have to do is periodically go in my ftp client&nbsp;and manually delete all the unused CSV files so they don't get too numerous.

 

Is there any way that I can create a CSV file for download without&nbsp;writing it to the disk? (That might sound like a dumb question, since I think I know the answer.) But I'd like to know if anyone might have an alternate solution.

 

 &nbsp;   Private Sub CreateSpreadsheet(ByVal inv As String)
&nbsp; &nbsp;   ; &nbsp; Dim i, c, r As Int32, ds As DataSet, colLabel(1) As String
&nbsp; &nbsp; &nbsp;   ; Dim s As New StringBuilder, temp As String


 &nbsp; &nbsp; &nbsp; &nbsp; ds = GetDataSet(inv)
 ; &nbsp; &nbsp; &nbsp;  GetHeader(colLabel, ds)
   ; &nbsp; &nbsp;  Dim rowcount As Int32 = ds.Tables(0).Rows.Count()
   ; &nbsp; &nbsp;  Dim colcount As Short = ds.Tables(0).Columns.Count

&nbsp; &nbsp; &nbsp; &nbsp;  For i = 0 To colcount - 1
 &nbsp; &nbsp;   ; &nbsp; &nbsp; c = i + 1
 &nbsp; &nbsp;   ; &nbsp; &nbsp; s.Append(colLabel(i) & ",&quot;)
   ; &nbsp; &nbsp;  Next

  ; &nbsp; &nbsp; &nbsp; s.Append(vbCr)

&nbsp;   ; &nbsp; &nbsp;  For r = 0 To rowcount - 1
 &nbsp; &nbsp;   ; &nbsp; &nbsp; For c = 1 To colcount
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   If Not IsDBNull(ds.Tables(0).Rows(r)(c - 1)) Then
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  temp = Replace(Trim( ds.Tables(0).Rows(r)(c - 1)), ",&quot;, "&quot;)
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  temp = Replace(temp, vbCr, " ")
&nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; temp = Replace(temp, vbLf, " ") & ",&quot;
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  Else
 &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp; temp = ",&quot;
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  End If
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; s.Append(temp)
   ; &nbsp; &nbsp; &nbsp; &nbsp; Next
 ; &nbsp; &nbsp; &nbsp; &nbsp;   s.Append(vbCr)
   ; &nbsp; &nbsp;  Next

  ; &nbsp; &nbsp; &nbsp; Response.ContentType = "application/vnd.msexcel&quot;
 &nbsp;   ; &nbsp;  Dim sSheetName As String
&nbsp; &nbsp; &nbsp;   ; Dim sDate As String = Month(Now.ToShortDateString) & Day(Now.ToShortDateString ) & Year( Now.ToShortDateString)
&nbsp; &nbsp;   ; &nbsp; sSheetName = ReportName & sStore & "_&quot; & sDate & ".csv"

 &nbsp; &nbsp; &nbsp; &nbsp; Dim FilePath As String = Server.MapPath("reports/&quot; & sSheetName)
 &nbsp; &nbsp; &nbsp;   CreateXLSFile(FilePath, s.ToString)
 &nbsp; &nbsp; &nbsp;   Response.Redirect(&quot;reports/" & sSheetName)

&nbsp; &nbsp;   End Sub

&nbsp;   ;  Private Sub CreateXLSFile(ByVal FilePath As String, ByVal TextToAdd As String)

 &nbsp; &nbsp;   ;  Try
   ; &nbsp; &nbsp; &nbsp; &nbsp; Dim sw As StreamWriter = New StreamWriter(FilePath)
 &nbsp; &nbsp; &nbsp;   ; &nbsp; sw.Write(TextToAdd)
&nbsp;   ; &nbsp; &nbsp; &nbsp;  sw.Close()
 &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As Exception

   ; &nbsp; &nbsp;  End Try

&nbsp;   ;  End Sub

--- List Settings --- http://aspadvice.com/lists/

--- List Settings ---
http://aspadvice.com/lists/

--- List Settings --- http://aspadvice.com/lists/
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )