I am trying to zip an Excel file and show the resulting zipped file to a user in a prompt with options to save, open and cancel.
I am using sharpZip .Net Library to zip the Excel file and the user prompt is failing to show when you click the link to Export to excel
.
The code is below. I am getting an in this method SingleGZip(file);, which says that the method has some invalid arguments.
public ActionResult ExportToExcel()
{
byte[] file;
string targetFilename = string.Format("{0}-{1}.xlsx", "Generated", "excel");
DataTable dt = common.CreateExcelFile.ListToDataTable(GetSearchDraftPRResults());
common.CreateExcelFile excelFileForExport = new CreateExcelFile();
file = excelFileForExport.CreateExcelDocumentAsStream(dt, targetFilename);
Response.Buffer = true;
SingleGZip(file);
//Stream memStream = new MemoryStream(file);
//using (ZipFile zipFile = new ZipFile())
//{
// zipFile.AddEntry("Generated-Excel.xlsx", "", memStream);
// Response.ClearContent();
// Response.ClearHeaders();
// Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");
// zipFile.Save(Response.OutputStream);
//}
//return
//byte[] zipFile = Compress(file);
return File(file, "application/vnd.ms-excel", targetFilename);
}
private byte[] SingleGZip(string source)
{
string target = source + ".gz";
using (Stream s = new GZipOutputStream(System.IO.File.Create(target)))
{
using (FileStream fs = System.IO.File.OpenRead(source))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
s.Write(buffer, 0, buffer.Length);
}
}
}