0

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);
            }

        }

    }
Tanner
  • 22,205
  • 9
  • 65
  • 83
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • Well, `file` is a `byte[]` and your `SingleGZip` method accepts a `string`. This is exactly what the compiler is telling you. What isn't clear? – Daniel Kelley Jun 13 '14 at 09:26

1 Answers1

0
private byte[] SingleGZip(string source) <-- give me a string

expectiong string as a parametr and you in your controller passing array of bites to SigleZip method.

byte[] file; <-- array of bytes
        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); <-- array of bytes
sylwester
  • 16,498
  • 1
  • 25
  • 33
  • hi is that procedure in correct way to zip excel files using shapzip library and I need to show the user prompt also .... – Glory Raj Jun 13 '14 at 09:34
  • I thing you should create zip file on your server using sharpZip and return to user file or just the 'path' to view and in View create download link using 'path' – sylwester Jun 13 '14 at 09:54