Download a file from page via HTTPS(SSL)

string _path = Server.MapPath(ConfigurationSettings.AppSettings["path"]);

if (File.Exists(_path))
{
    string data = File.ReadAllText(_path);

    byte[] byteArr = Encoding.UTF8.GetBytes(data);
    MemoryStream stream = new MemoryStream(byteArr);

    // downloading file via HTTPS(SSL) will not work w/o clearing resposne.
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.AddHeader("Content-Disposition",
                       "attachment; filename=" + Path.GetFileName(_path) + "\"");
    Response.AddHeader("Content-Length", byteArr.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.BinaryWrite(byteArr);
    Response.End();
}

Advertisements