Importing tab-delimited text into a Listbox (Web)

Hello Everyone,

I would like users to be able to import a tab-delimited spreadsheet into my app, check it in a listbox, and then upload it to the database.

There is sample code for doing the importing just like I would like to in the Language Reference right here

(Copy/Pasted Code from the linked page)

Var f As FolderItem
Var textInput As TextInputStream
Var rowFromFile, oneCell As String
Var i As Integer
f = FolderItem.ShowOpenFileDialog("text/plain") // defined as a FileType
If f <> Nil And f.Exists Then
  Var tab As String = ChrB(9)
  textInput = TextInputStream.Open(f)
  While Not textInput.EndOfFile
    rowFromFile = textInput.ReadLine
    
    // Set
    If ListBox1.ColumnCount < rowFromFile.CountFields(tab) Then
      ListBox1.ColumnCount = rowFromFile.CountFields(tab)
    End If
    
    ListBox1.AddRow("")
    For i = 1 To rowFromFile.CountFields(tab)
      oneCell = rowFromFile.NthField(tab, i)
      ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, i - 1) = oneCell
    Next
  Wend
  textInput.Close
End If

Unfortunately, it’s only for Desktop.

I’ve tried to use a WebFileUploader so the user can choose the file and upload, but then I’m at a loss as to how to convert the Sample Code to something that will work on the Web.

Any suggestions or help would be greatly appreciated.

Thank you!

beside the FileUploader1 in the design window you have a button and at pressed event
you start the upload with
FileUploader1.StartUpload

FileUploader1 have a event UploadFinished

Sub UploadFinished(Files() As WebUploadedFile) Handles UploadFinished
  
  System.DebugLog "Upload Finished"
  
  For Each f As WebUploadedFile In Files
'here are your folderitems f.File and only valid inside this method, outside of this method they are gone, if you need this afterwards you have to save it.
  Next
End Sub

rewrite your sample code as a method with a folderitem and WebListBox as input argument.

1 Like

Thanks MarkusR. I didn’t do exactly that, but I found a way to make it work. Please feel free to give me any tips to clean this up.

Var outputFile As FolderItem
Var output As BinaryStream
Var textInput As TextInputStream
Var rowFromFile, oneCell As String
Var i As Integer

For Each file As WebUploadedFile In files
  Try
    outputFile = New FolderItem(file.Name)
    output = BinaryStream.Create(outputFile, True)
    output.Write(file.Data)
    output.Close
    if outPutFile <> Nil then
      Var tab As String = ChrB(9)
      
      textInput=TextInputStream.Open(outputFile)
      while not textInput.EndOfFile
        rowFromFile=textInput.ReadLine
        
        //set the column count in the listbox
        if Listbox1.ColumnCount<rowFromFile.CountFields(tab) then
          Listbox1.ColumnCount=rowFromFile.CountFields(tab)
        end if
        
        ListBox1.AddRow("")
        For i = 1 To rowFromFile.CountFields(tab)
          oneCell = rowFromFile.NthField(tab, i)
          ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, i - 1) = oneCell
        Next
      Wend
    end if
    textInput.Close
  Catch e As IOException
    Continue
  End Try
Next

One more question: I’ll need to delete the first row, since that will likely be a header. Is there an easy way to do it on import, or should I just do it once it’s in the listbox?

creating a file copy is not necessary in the UploadFinished method.
it is only necessary if you will process the files after UploadFinished method.
after import you could remove the first row with the header data, otherwise you need to count the rows or using a flag.
if you are reading all text of the file into a string you could use split by row / linebreak and ignore the first one.

1 Like