Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

How to Download File

I have been following these links all listed below, i found the best way to write this SMALL create Excel and Download function. ( Using EPPlus for Excel )

It runs through the code perfectly without error every time I run this but does not "Kick out" the file to be downloaded ( in a save as dialogue or w/e ).

public ActionResult ShowReport()
    {
        using (var stream = new MemoryStream())
        {

            ExcelPackage pck = new ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("Sample1");

            ws.Cells["A1"].Value = "Sample 1";
            ws.Cells["A1"].Style.Font.Bold = true;
            var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
            shape.SetPosition(50, 200);
            shape.SetSize(200, 100);
            shape.Text = "Sample 1 text text text";

            var fileDownloadName = "sample.xlsx";
            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";//System.Net.Mime.MediaTypeNames.Application.Octet
            var fileStream = new MemoryStream();
            pck.SaveAs(fileStream);
            fileStream.Position = 0;

            var fsr = new FileStreamResult(fileStream, contentType);
            fsr.FileDownloadName = fileDownloadName;

            byte[] fileBytes = ReadToEnd(fileStream);
            string fileName = "example";
            return File(fileBytes, contentType, fileName);
        }
    }

What am I doing wrong / missing? - Must i write that Dialogue myself?

PN: I have also attempted this way

 byte[] fileBytes = ReadToEnd(fileStream);
 string fileName = "example";
 return File(fileBytes, contentType, fileName);

ofcourse i had to figure out how to convert Stream to Byte but it also did not show anything.

Image of Chrome's Network Development Tool enter image description here Sorry about the small image ( if you can't see it scroll in with ctl+MouseWheel ) if your in a supporting browswer.

Upvotes: 2

Views: 4232

Answers (2)

David
David

Reputation: 219057

(In response to the comment thread above.)

From the image posted it looks like the actual file request (the last one in the list) is coming from JavaScript code instead of from a normal document-level request. Given this, it's highly likely that the server-side code is working correctly and returning the correct response.

However, since it's an AJAX request, the browser doesn't actually know what to do with the response. There are some potential solutions here. Ideally, you'll want to make this a normal request and remove AJAX from the picture if possible. If that's not an option, you can still initiate a document-level request from JavaScript. Something as simple as this:

window.location = '@Url.Action("Method", "Controller")';

This would be initiated from JavaScript code as it currently is, but would be for the whole browser instead of an AJAX request. That should do the trick.

Upvotes: 3

Nunners
Nunners

Reputation: 3047

Using the memory stream you have you can simple pass that to the Response object once you have saved the Excel Package

Code:

    Response.AddHeader("content-disposition", "attachment;filename=FILENAME.xlsx")
    Response.Charset = String.Empty
    Response.ContentType = "application/ms-excel"
    Response.BinaryWrite(stream.ToArray())
    Response.End()

Upvotes: -1

Related Questions