# Can I use Webfile for this?

**URL:** <https://forum.xojo.com/t/can-i-use-webfile-for-this/53022>\
**Category:** Web\
**Created:** [March 31, 2020, 5:42pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022 "2020-03-31T17:42:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Albrecht](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_albrecht/32/817_2.png) [@Richard\_Albrecht](https://forum.xojo.com/u/Richard_Albrecht)\
**Post date:** [March 31, 2020, 5:42pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/1 "2020-03-31T17:42:13Z")

</div>

OK trying to figure out:

This creates a csv file that will be downloaded. The calling procedure will create the Recordset then Call this and then do showurl.

I have a function that creates a text cvs file.

Can I make tos as Webfile, a property of the window?

Not sure what to do?

[code]  
DoCVS(rs as RecordSet)

dim tos as textOutputStream  
dim s as string  
dim i, j as integer

tos = lfFile.CreateTextFile

s=""""

Dim lnEmailCol As Integer

for i = 1 to rs.FieldCount  
s = s +rs.IdxField(i).Name + “”","""  
next  
tos.WriteLine(s.left(s.len-2))

for i=1 to rs.RecordCount 'for each row  
s="""" 'build line to save  
for j=1 to rs.FieldCount 'for each column  
s = s + ChkStr(rs.IdxField(j).StringValue) + “”","""

next  
tos.WriteLine(s.left(s.len-2))  
rs.MoveNext 'save line  
next

tos.Close[/code]

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [March 31, 2020, 6:02pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/2 "2020-03-31T18:02:01Z")

</div>

Yes, prepare your CSV entirely then put the string in the `WebFile.Data` property.

You’ll need to rewrite your code such that you’re not generating the CSV during the write operation. I would recommend this as a general best practice too.

It is possible to avoid messing around with String.Left if you construct the row as an array kind of like this:

```auto
dim rs as RecordSet = MySelect
dim tarsRows() as String

while not rs.EOF
  dim tarsThisRow() as String
  for i as Integer = 1 to rs.FieldCount
    tarsThisRow.Append(rs.IdxField(i).StringValue)

  next i

  // Build the row and append it to the complete data
  dim sThisRow as String = Join(tarsThisRow, ",")
  tarsRows.Append(sThisRow)

  rs.MoveNext

wend

// Generate the complete CSV from the rows
dim sCSV as String = Join(tarsRows, EndOfLine.Unix)

// Prepare the WebFile
moMyWebFileInstance.Data = sCSV
```

Notes about this code:

- This was written in the forum post editor, corrections may be necessary
- Depending on your data, you may need to apply quotes to a field
- This is a really flimsy CSV generation method, there may be better options

---

<div class="post-metadata">

**Author:** ![Richard\_Albrecht](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_albrecht/32/817_2.png) [@Richard\_Albrecht](https://forum.xojo.com/u/Richard_Albrecht)\
**Post date:** [March 31, 2020, 6:29pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/3 "2020-03-31T18:29:14Z")

</div>

Getting NilObjectException on

moMyWebFileInstance.Data = sCSV

---

<div class="post-metadata">

**Author:** ![Richard\_Albrecht](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_albrecht/32/817_2.png) [@Richard\_Albrecht](https://forum.xojo.com/u/Richard_Albrecht)\
**Post date:** [March 31, 2020, 6:34pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/4 "2020-03-31T18:34:00Z")

</div>

Needed to instantiate it.

---

<div class="post-metadata">

**Author:** ![David\_Cox](https://forum.xojo.com/user_avatar/forum.xojo.com/david_cox/32/227_2.png) [@David\_Cox](https://forum.xojo.com/u/David_Cox)\
**Post date:** [April 1, 2020, 7:52am UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/5 "2020-04-01T07:52:08Z")

</div>

I zip every file I offer to download, except PDF (already compressed), so the browser doesn’t try to grab and display it.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:20pm UTC](https://forum.xojo.com/t/can-i-use-webfile-for-this/53022/6 "2021-04-29T16:20:29Z")

</div>


