title: Return file download from byte[]
body: <p>This code</p>
  
  <pre><code>string xml = XmlHelper.ToXml(queryTemplate);
  
  byte[] xmlb = StringHelper.GetBytes(xml);
  
  var cd = new System.Net.Mime.ContentDisposition
  {
      // for example foo.bak
      FileName = String.Format("{0}_v{1}.xml", queryModel.Name, queryModel.Version),
  
      // always prompt the user for downloading, set to true if you want
      // the browser to try to show the file inline
      Inline = false,
  };
  Response.AppendHeader("Content-Disposition", cd.ToString());
  return File(xmlb, "application/xml");
  </code></pre>
  
  <p>Turns out the string encoding is incorrect after it's converted into <code>byte[]</code></p>
  
  <p>So I need to put the <code>string</code> immediately into file, like this</p>
  
  <pre><code>FileStream xfile = new FileStream(Path.Combine(dldir, filename), FileMode.Create, System.IO.FileAccess.Write);
  hssfwb.Write(xfile);
  </code></pre>
  
  <p>But I don't want to do this, I don't need the file after the download. I just need to return it to the browser as file download and don't want to have to deal with file deletion afterward which can become pretty hectic when there's a lot of request.</p>
  
  <p>How to correct the character encoding from <code>string</code> to <code>byte[]</code> and correctly return it to the browser?</p>
  
  <p>The <code>GetBytes</code> function looks like this</p>
  
  <pre><code>public static byte[] GetBytes(string str)
  {
      byte[] bytes = new byte[str.Length * sizeof(char)];
      System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
      return bytes;
  }
  </code></pre>