Skip to content

Instantly share code, notes, and snippets.

@JRondeau16
Created March 12, 2015 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRondeau16/3dfc00bfe73af6eab362 to your computer and use it in GitHub Desktop.
Save JRondeau16/3dfc00bfe73af6eab362 to your computer and use it in GitHub Desktop.
ExportControl
using System;
using System.IO;
using System.Web;
using Sitecore.IO;
using Sitecore.Form.Core.Utility;
namespace MyProject
{
public partial class ExportBinary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var text = Session["exportcontent"] as byte[]; //may need to change depending on the type of file
var filename = Session["filename"] as string;
if (string.IsNullOrEmpty(filename) || text == null || text.Length.Equals(0))
return;
Sitecore.Diagnostics.Log.Info(string.Format("Exporting my custom file {0}", filename), this);
var tempFileName = WebUtil.GetTempFileName();
File.WriteAllBytes(tempFileName, text); //may need to change depending on the type of file
var fileSize = FileUtil.GetFileSize(tempFileName);
WriteCacheHeaders(filename, fileSize);
Response.TransmitFile(tempFileName, 0L, fileSize);
Response.End();
}
private void WriteCacheHeaders(string filename, long length)
{
HttpResponse response = HttpContext.Current.Response;
response.ClearHeaders();
response.AddHeader("Content-Type", (string)Session["contentType"] ?? "application/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.AddHeader("Content-Length", length.ToString());
response.Cache.SetNoStore();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment