Archived Forum Post

Index of archived forum posts

Question:

Best way to stream a large csv file to the broweser

Jun 29 '15 at 15:47

Hello,

I am revamping a user report from html to csv. Depending on the user's options this report can be very large. I would like to stream the report to the browser the same way that I can stream or chunk a pdf or zip file to the browser. I think I am on the right track but I have two issues. First, Excel reports the file is incomplete and second, all the the data is on a single line

Thanks,

Erik

Code

Set oStr = Server.CreateObject("Chilkat_9_5_0.StringArray")
oStr.LoadFromText oCsv.SaveToString

With Response
  .Buffer = true
  .Expires = 0
  .Clear
  .ContentType = "text/csv"
  .AddHeader "content-length", oStr.Length -1
  .AddHeader "content-disposition", "inline; filename=""Report.csv"""
  .flush

End With

Idx = 0: Ctr = 0
For Idx = 0 To oStr.Count -1
  Ctr = Ctr + 1
  If Ctr = 100 Then
    Response.Flush
    Ctr = 0

  End If

  Response.Write oStr.GetString(Idx) & vbLf

Next
Response.Flush
oStr.Clear
Set oStr = Nothing

Accepted Answer

A few thoughts:

  1. Perhaps Excel is expecting CRLF instead of LF line-endings? Try setting the CSV Crlf property to 1.
  2. Similarly to #1, you are appending vbLf to each Response.Write - try vbCrLf instead.
  3. Again in the same vein as above - try setting the StringArray object CRLF property to 1.
  4. You are setting the Content-Length header to the size-1, but I think it should be set to the size.
  5. Not sure if you need the trailing vbCrLf on the last write? I would experiment with and without.