How To Download and Update Documents


The following step-by-step tutorial explains how to download and update documents that are stored in a database. Download and upload buttons are added to the document details screen. The download button allows a user to save a document to disk, the upload button allows a user to replace the existing document with another one.

This tutorial extends the How To Display Office Documents When Office is Unavailable tutorial. Complete that tutorial first before starting.

1) Open the LightSwitch project created in the How To Display Office Documents When Office is Unavailable tutorial in Visual Studio 2010.

2) Open the DocumentDetails screen in the designer and select the File For Display element in the designer. Right click the element and select Add Button...

AddButton.png

3) Name the method Upload and select Ok.

AddButtonDialog.png

4) Add another button having a method named Download. Your designer should look similar to the following image.

DesignerButtons.png

5) Right-click the Download button and select Edit Execute Code.

EditExecute.png

6) Enter the following code in the Download_Execute method:

C#
using System.Windows.Controls;
using Microsoft.LightSwitch.Threading;

partial void Download_Execute()
{
  Dispatchers.Main.BeginInvoke(() => {
    var dlg = new SaveFileDialog();
    if (dlg.ShowDialog() == true) {
      using (var stream = dlg.OpenFile()) {
        // save original uploaded File
        stream.Write(this.Document.File, 0, this.Document.File.Length);
      }
    }
  });
}

VB.NET
Imports System.Windows.Controls
Imports Microsoft.LightSwitch.Threading

Private Sub Download_Execute()
  Dispatchers.Main.Invoke(
  Sub()
    Dim dlg = New SaveFileDialog()
    If dlg.ShowDialog = True Then
      Dim stream = dlg.OpenFile
      Using (stream)
        ' save original uploaded File
        stream.Write(Me.Document.File, 0, Me.Document.File.Length)
      End Using
    End If
  End Sub)
End Sub

7) Go back to the DocumentDetails designer and edit the execute code for the Upload button.

C#
using Microsoft.LightSwitch.Threading;
using System.Windows.Controls;
using DocumentToolkit.Client;

partial void Upload_Execute()
{
  var file = SelectFile();
  if (file != null) {
    using (var reader = DocumentReader.Create(file)) {
      // update file binary
      this.Document.File = reader.ToBinary();

      // update xps binary
      this.Document.FileXps = null;
      if (reader.RequiresConversionToXps) {
        // conversion needed, get the XPS binary
        this.Document.FileXps = reader.ToXpsBinary();
      }
    }
  }
}

private FileInfo SelectFile()
{
  FileInfo result = null;
  Dispatchers.Main.Invoke(() => {
    var dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == true) {
      result = dlg.File;
    }
  });
  return result;
}

VB.NET
Imports Microsoft.LightSwitch.Threading
Imports System.Windows.Controls
Imports DocumentToolkit.Client

Private Sub Upload_Execute()
  Dim file = SelectFile()
  If Not file Is Nothing Then
    Dim reader = DocumentReader.Create(file)
    Using reader
      ' update file binary
      Me.Document.File = reader.ToBinary
      ' update xps binary
      Me.Document.FileXps = Nothing
      If reader.RequiresConversionToXps Then
        ' conversion needed, get the XPS binary
        Me.Document.FileXps = reader.ToXpsBinary
      End If
    End Using
  End If
End Sub

Private Function SelectFile()
  Dim result As FileInfo = Nothing
  Dispatchers.Main.Invoke(
  Sub()
    Dim dlg = New OpenFileDialog()
    If dlg.ShowDialog = True Then
      result = dlg.File
    End If
  End Sub)

  SelectFile = result
End Function

Note: you'll notice that the SelectFile method is the same as the one used in the SearchDocuments screen. Some kind of code re-use would be nice.

8) Compile and run the application. Navigate to the SearchDocuments screen, select a document and now you can download the document to disk or upload a new version of your documents with the newly added buttons.

UploadDownload.png


Next step: learn how to add an Open With button to open documents using an external application in How To Open Documents With External Applications.

Last edited Apr 18, 2013 at 4:42 PM by kozw, version 8

Comments

kozw Jul 12, 2011 at 11:59 AM 
Unfortunately setting a default filename to the save dialog is not supported in SL4. It looks like it will be possible in SL5.

A new tutorial demonstrating how to open documents with external apps has just been added.

mribeiral Jul 11, 2011 at 9:36 PM 
Would anybody help me with the code to use the file name in the database as the default name when saving the file to disk?
Also, if anyone could provide me with the code to open the file without saving first to disk I would appreciate it.