context.ReturnFileAsync

Hello,

I am trying to download a file with ReturnFileAsync on a click on a button but it does not seem to work, did I forget something?

My button is like this :
<dot:Button Text=“Download” Click=“{command: OnDownloadFile()}” />

And my method like this :
public async Task OnDownloadFile()
{
try
{
var Bytes = File.ReadAllBytes(@“C:\Temp\File.csv”);
await Context.ReturnFileAsync(Bytes, “File.csv”, “application/octet-stream”);
}
catch (System.Exception ex)
{
MessageErreur = ex.Message;
}
}

But I get the exception “Request interrupted: ReturnFile (File.csv)”

Context.ReturnFile* and also Context.Redirect* internally work by throwing DotvvmInterruptRequestExecutionException that you should rethrow.

public async Task OnDownloadFile()
{
  try
  {
    var Bytes = File.ReadAllBytes(@“C:\Temp\File.csv”);
    await Context.ReturnFileAsync(Bytes, “File.csv”, “application/octet-stream”);
  }
  catch (DotvvmInterruptRequestExecutionException) 
  {
    throw;
  }
  catch (System.Exception ex)
  {
    MessageErreur = ex.Message;
  }
}

Maybe we can make a Roslyn analyzer to detect this pattern.

Cool, that works fine indeed now !

Thanks a lot !

However, I noticed something when I was catching all types of exceptions.
Maybe this question should be posted on another thread but I give you the details here.
My error message that was assigned to a field MessageErreur was not displayed on my page.

In my page I have the following :
<dot:Literal Text=“{value: MessageErreur}” style=“color: Red; font-size: 20px; font-weight: normal;” />

In my ViewModel I declared the field as follows :
public string MessageErreur { get; set; }

The field is then reset in the Init method :
public override Task Init()
{
try
{
MessageErreur = string.Empty;

But when I change its value in the OnDownloadFile method, the page does not show any value for this field.