Back

Initiate Binary File Download from Button Click

Here's HowTo trigger a "Save As..." dialog from a regular Canavs button click handler, allowing more work control over the result.

public void btnDownLoadClick(object sender, EventArgs e){

  // Do Work ... such as track / log

  // Make the response send a file back as a download
  // 
  Response.Clear();
  Response.Charset = "";
  Response.ContentEncoding = System.Text.Encoding.UTF8;

  // Setting Excel as the Content type 
  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

  // This HEADER triggers the "Save As..."
  Response.AddHeader(
    "content-disposition", 
    // The name of the file when saved by the client
    "attachment;filename=Output.xlsx"
  );

  Response.WriteFile(
    Net4orce.Core.cApplicationPath + "file_name.xlsx"
  );

  Response.Flush();
  Response.End(); 

}