2

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.

Community
  • 1
  • 1
Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • The last line of code (that "did not show anything") is the correct approach. Please elaborate on how it didn't show anything. Was there a response from the server at all? When you debug this, where does it fail? – David Sep 24 '13 at 13:09
  • Well i have my browswer open in Chrome using HTTP Headers and other extensions and a few other extensions as well as having open the Developer Tools, that won't show any signs of anything "Gone Wrong". How else can I tell, yes i stepped through my code inside of VS, without flaw? – Don Thomas Boyle Sep 24 '13 at 13:11
  • In the browser's debugging tools, what was the response from the server? Did an actual file come through and it was empty and/or corrupt? If you save the file server-side is it a valid file? – David Sep 24 '13 at 13:14
  • I have not attempted to save it server side, please allow me some time to try that, as far as the Browser, its not showing me anything about any file. ( Console is empty ). – Don Thomas Boyle Sep 24 '13 at 13:17
  • That's strange that the browser doesn't show *anything*. There should be *something* (on a network tab probably? I'm not familiar with Chrome's tools UI) describing the request sent to and response received from the server. How are you making the request? Is it a link, a form post, maybe you're trying to do this via AJAX? (If the latter, than *that* would likely be the problem, instead of anything in the server-side code. Since AJAX responses aren't handled the same way by the browser as normal responses. There wouldn't be a dialog to save the file.) – David Sep 24 '13 at 13:19
  • I'm coming form a View an Action Link more Specifically. No Ajax or any other script for that matter, simple link. – Don Thomas Boyle Sep 24 '13 at 13:25
  • Looking at that image, is that last request the one for the file? (The content type is cut off, but it looks like the only one with a potentially Excel content type.) What concerns me is that "jquery" is listed as the "initiator" of the request. You're sure it's coming from clicking on a link and not from JavaScript code? – David Sep 24 '13 at 13:28
  • Well ;/ aparantly i had overlooked some code that uses JQuery to call the dang Method.. Thanks for the catch and help aswell is there no way around this? - also please answer so I can accept it. Thanks, file downloads just fine now. – Don Thomas Boyle Sep 24 '13 at 13:33

2 Answers2

3

(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.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
-1

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()
Nunners
  • 3,047
  • 13
  • 17
  • 1
    Returning an `ActionResult` from a controller action is *vastly preferred* over directly writing to the `Response` object. Your approach will tightly couple the controller to the HTTP context, making it much less portable and very difficult to unit test. – David Sep 24 '13 at 13:13