The simplest way to Download binary data in LightSwitch
Most line of business apps needs the ability to store and retrieve binary data (e.g. a pdf, an excel file, a picture) in and from the application. Typically, these files associated with a specific entity; for example a contract is associated with Customer abc.
Obviously, these files should adhere to the security rules of the application in such a way a user can only download files associated with records that the current user can manage.
The ability to use web-api in LightSwitch, in combination with the ServerApplicationContext really simplifies this complete process.
The Data Model
The data model needed for storing binary data is pretty simple. The most crucial part is to separate the binary data itself (which can be large) from the meta data of the binary data. By doing so, it will become more comfortable to visualize file meta data without being bothered by a heavy load of data.
So, we have a FileMetaData table:
This table has a 1 to 0..1 relation with the FileStore table:
The FileMetaData table can be linked via a 0..1 to many relation with any entity type that has the need for attaching file data. In my example I provide the customer table to attach 0..n files to each customer:
How to transport the data from the server to the client?
Intuitively, one would think that the most simple way to get the data to the client is simply to request the record containing the file data. That’s true when the data would simply stay inside the client application, but in most cases (as it is here), we want that the user can save the data to his local disk.
Obviously, it’s possible to foresee a File dialog where the user can select a file location and then save the binary stream to disk, but this is quite a lot of code and furthermore the user will need to type the file name in the File dialog. Silverlight has no way to do this differently.
Instead, we would like the following:
Luckily, this is possible with web-api.
setting up a web-api File Controller
First, add a global.asax file to the server project:
protected void Application_Start(object sender, EventArgs e)
Make sure the server project has a reference to System.Web.Http, System.web.Http.WebHost and System.Net.Http
Seconly, add a web api class to your server:
if (myFileStoreEntry == null)
I’m using a small base class to expose the ServerApplicationContext to the web api class:
public ServerApplicationContext Context
public ServerApplicationContext GetContext()
Oh my goodness, that’s all server side.
Consuming the File Controller
You can already consume the web-api controller, simply from your browser:
We presume here that we have already a binary data record in the FileStore table with Id 11.
Of course, we want to consume it from the silverlight client as well:
So, imagine we have something like this in mind:
So, a simple out of the box list Detail screen, which has no code at all, except :
partial void Download_Execute()
What can I say? Can this simpler? In a next post I’ll cover uploading files via web-api or via a table.