Saturday, January 31, 2009

Send file to browser

private void SendFile(string fileText, string fileName, string contentType) {
  this.Response.Clear();
  this.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
  this.Response.ContentType = contentType;
  this.Response.Write(fileText);
  this.Response.End();
}
Typical content types: Text "text/plain" PDF "application/pdf" Got it from: http://coercedcode.blogspot.com/2007/08/sending-files-and-file-contents-to.html. Starting the download with a button inside an UpdatePanel? Register the button for a regular, not partial postback:
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(exportButton);

No comments: