The most important thing to notice is that the user has to 'confirm' the download twice: When he first clicks the button, and again when it has been downloaded, and the user can chose to 'Save' or 'Cancel' to open the dialog box, where the user determines where on his desktop he want to save the file.

Step 1. Create the object that is returned from the database containing the file you want to download. The file object has two attributes: The name of the file, and the file itself, which is saved as a bite array, and is of type SqlBytes:

Step 4: In the ViewModel, create the command that executes when the "Download File" button is clicked, and that calls the GetFile() method described in step 2.

You will have to notice several things: First, the executeDownloadFile calls the GetFile(), and when the call is completed, the WebService_GetFileCompleted method is called, and return value (the file) is saved in a newly created object of type FileDTO , i.e. the class you created in step 1.

//Property bound to the button
private DelegateCommand downloadFile;
public DelegateCommand DownloadFile
{
get
{
if (downloadFile == null)
downloadFile = new DelegateCommand(executeDownloadFile, canExecuteDownloadFile);
return downloadFile;
}
}
//Defines if the button is clickable
private bool canExecuteDownloadFile(object parameter) //Definerer om knappen er klik-bar.
{
return true; //always clickable
}
private void executeDownloadFile(object parameter)
{
//Shows progress bar while downloading
ShowText = "Downloading file";
BusyWindow = true;
WebService = new Service1Client();
//defines the method that is called when it returns with a value
WebService.GetFileCompleted += new EventHandler<GetFileCompletedEventArgs>(WebService_GetFileCompleted);
//Calls the method
WebService.GetFileAsync(AID);
}
//Method is called when the Model has returned with a value
void WebService_GetFileCompleted(object sender, GetFileCompletedEventArgs e)
{
// Call the method CallBackFromService with the result (the file) as a parameter
CallBackFromService(e.Result);
}
//Create new instance of FileDTO.
FileDTO fileDTO;
public void CallBackFromService(FileDTO fileDTOCallBack)
{
Set to the local version of the fileDTO, close progressbar
fileDTO = fileDTOCallBack;
BusyWindow = false;
//Makes button from (3) visible: The user has to confirm that he wants to download the file.
AfterDownload = Visibility.Visible;
}

Step 6: After the CallBackFromService has been called, the user must confirm that he wants to download the file. The button in "Save File" in window (3) shown above is bound to the command SaveAttachment: