I need to write an export function that will create a .csv file and place it on the users PC. Creating the data is not a problem, but how to write it to a file (maybe the downloads folder on a Windows machine - not sure the equivalent on a MAC or Linux machine).
Can anyone point me in the right direction?
Thank you,
Tim
Public Function RStoCSV(Data as RecordSet, Optional Delemiter as String = ";") as String
dim CSV() as String
'Header
dim head() as string
for i as integer = 1 to data.FieldCount
head.Append(data.IdxField(i).Name )
next i
CSV.Append(Join(head, Delemiter))
'content
While Not data.EOF
dim row() as string
for i as Integer = 1 to data.FieldCount
dim inhalt as string = data.IdxField(i).StringValue.DefineEncoding(Encodings.UTF8)
inhalt = ReplaceAll(inhalt, chr(10), " | ")
inhalt = ReplaceAll(inhalt, chr(13), "")
inhalt = ReplaceAll(inhalt, chr(9), " ")
inhalt = ReplaceAll(inhalt, Delemiter, " ")
inhalt = ReplaceAll(inhalt, """", "\""")
row.Append(inhalt)
next i
CSV.Append(Join(row, Delemiter))
data.MoveNext
Wend
data.MoveFirst
dim outp as string = Join(CSV, EndOfLine)
outp = ReplaceAll(outp, chr(10), "")
outp = ReplaceAll(outp, EndOfLine, "")
return Join(CSV, EndOfLine)
End Function
Download the File:
place a property into the app object:
Public Property TextFile as WebFile
then add this function to the app object as well:
Public Sub DownloadFile(Content as String, Filename as string, optional Format as string = "csv", optional enc as string = "utf8")
TextFile = New WebFile // TextFile is a property of the web page
TextFile.MimeType = "text/plain"
TextFile.ForceDownload = True // If False, the browser may try to display the file instead of download it
TextFile.FileName = Filename + "." + Format
TextFile.Data = ConvertEncoding(Content, Encodings.UTF8)
// use different coding if wanted
if enc = "ascii" then TextFile.Data = ConvertEncoding(Content, Encodings.ASCII)
if enc = "ansi" and Session.Platform = WebSession.PlatformType.Windows then TextFile.Data = ConvertEncoding(Content, Encodings.WindowsANSI)
webpage1.ShowURL(TextFile.URL) // This causes the file to be downloaded
End Sub