Accessing files

These questions are related to file handling, which make use of the FolderItem class.

How do I load and save data to a file?

There are several classes to help you load and save your data. If you are working with text files, you use the TextInputStream class to load data and the TextOutputStream class to save data. If you are working with binary data (or data with a fixed-size file format) you can use the BinaryStream class.

This code prompts the user to choose a file and then its contents are read and displayed in a text area:

Var f As FolderItem = FolderItem.ShowOpenFileDialog("")
If f <> Nil Then
  If f.Exists Then
    // Be aware that TextInputStream.Open could raise an exception
    Var t As TextInputStream
    Try
      t = TextInputStream.Open(f)
      t.Encoding = Encodings.UTF8
      TextArea1.Text = t.ReadAll
    Catch e As IOException
      MessageBox("Error accessing file.")
    End Try
    t.Close
  End If
End If

This code saves text in a TextArea to a text file in the Documents folder:

Var Documents As FolderItem = SpecialFolder.Documents
If Documents <> Nil Then
  Var f As FolderItem = Documents.Child("Sample.txt")
  If f <> Nil Then
    Try
      // TextOutputStream.Create raises an IOException if it can't open the file for some reason.
      Var t As TextOutputStream = TextOutputStream.Create(f)
      t.Write(TextField1.Text.ConvertEncoding(Encodings.UTF8))
      t.Close
    Catch e As IOException
      // handle
    End Try
  End If
End If

How do I list files in a folder?

The FolderItem class is used to access the file system. You can use its Count property and Item method to iterate through files contained in a folder.

This code puts the name of all files on the Desktop into a ListBox:

Var file As FolderItem
Var folder As FolderItem = SpecialFolder.Desktop
Var fileCount As Integer = folder.Count
For i As Integer = 1 To fileCount
  file = folder.ChildAt(i)
  If file <> Nil Then
    ListBox1.AddRow(file.Name)
  End If
Next

How do I upload and download files in a web app?

To allow users to upload files to you web app, you use the WebFileUploader control. This control provides a user interface for the user to select and add files to a list to be uploaded. You then add a button to the page that calls the WebFileUploader.StartUpload method on the control to actually upload the files to your web app on the server. When the upload has finished, the :ref:UploadFinished`<webfileuploader.uploadfinished>` event handler is called where you can get a list of the files that were uploaded so that you can process them as necessary.

To make a file available for download, you first have to create a WebFile that refers to the file on the drive. This WebFile should be a Property of the WebPage so that it does not go out of scope while the file is downloading, which would cause an incomplete download. You then want to set the WebFile.ForceDownload property to True so that the file is downloaded rather than displayed in the browser. To start the download, you call ShowURL and pass in the URL of the WebFile that you created.

For more information:

  • Examine the WebFileUploader example project (Examples/API/User Interface/Web) that shows you how to upload files to a web app.

  • Examine the Downloading example project (Examples/Topics/Web/Downloading Files) that shows you how to download files from a web app.

  • Watch the Uploading and Downloading Files with a Web App video.

How do I create a folder?

To create a folder, you first create a FolderItem that points to where you want the folder. Then you call the FolderItem.CreateFolder method.

This code creates a folder called "MyData" in the Documents folder:

Var myData As FolderItem = SpecialFolder.Documents.Child("MyData")
myData.CreateFolder