Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Offhand, what you're doing should work. I've successfully done similar in the past, although I used a repeater and LinkButtons.</p> <p>The only thing I can see that's different is that you're using <code>Response.Write()</code> rather than <code>Response.OutputStream.Write()</code>, and that you're writing text rather than binary, but given the <code>ContentType</code> you specified, it shouldn't be a problem. Additionally, I call <code>Response.ClearHeaders()</code> before sending info, and <code>Response.Flush()</code> afterward (before my call to <code>Response.End()</code>). </p> <p>If it will help, here's a sanitized version of what works well for me:</p> <pre><code>// called by click handler after obtaining the correct MyFileInfo class. private void DownloadFile(MyFileInfo file) { Response.Clear(); Response.ClearHeaders(); Response.ContentType = "application/file"; Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.FileName + "\""); Response.AddHeader("Content-Length", file.FileSize.ToString()); Response.OutputStream.Write(file.Bytes, 0, file.Bytes.Length); Response.Flush(); Response.End(); } </code></pre> <p>You may want to consider transferring the file in a binary way, perhaps by calling <code>System.Text.Encoding.ASCII.GetBytes(viewXml);</code> and passing the result of that to <code>Response.OutputStream.Write()</code>.</p> <p>Modifying your code slightly:</p> <pre><code>protected void btnDownload_Click(object sender, EventArgs e) { string viewXml = exporter.Export(); byte [] bytes = System.Text.Encoding.ASCII.GetBytes(viewXml); // NOTE: you should use whatever encoding your XML file is set for. // Alternatives: // byte [] bytes = System.Text.Encoding.UTF7.GetBytes(viewXml); // byte [] bytes = System.Text.Encoding.UTF8.GetBytes(viewXml); Response.Clear(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment; filename=views.cov"); Response.AddHeader("Content-Length", bytes.Length.ToString()); Response.ContentType = "application/file"; Response.OutputStream.Write(bytes, 0, bytes.Length); Response.Flush(); Response.End(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload