1

Controller method:

public FileResult FromModel(ExcelWorkBook model)
{
    byte[] fileBytes;
    var reportStream = ExcelCreator.Create(model);
    try
    {
        fileBytes = reportStream.ToArray();
        var fileName = "some_crazy_filename.xlsx";

        var encoding = System.Text.Encoding.UTF8;
        Response.Charset = encoding.WebName;
        Response.HeaderEncoding = encoding;
        Response.AppendHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { FileName = fileName, Inline = true }.ToString());
        return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }
    catch (Exception) { throw; }
    finally
    {
        reportStream.Close();
        reportStream.Dispose();
    }
}

Angular2 method:

this.http.post('/Excel/FromModel', postData, { headers: headers, responseType: ResponseContentType.ArrayBuffer })
    .catch(this.handleError)
    .subscribe(x => {
        console.log(x);
        let blob = new Blob([x.arrayBuffer()], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        fileReader.readAsDataURL(blob);
        fileReader.onloadend = function (readerEvent) {
            window.open(fileReader.result);
        };
    });

The file is downloaded perfectly, but the filename is always download.xlsx any idea what I am missing? I feel like I am missing something easy... thank you!

6
  • 1
    Why bother downloading the file asynchronously if you're only going to save it via window.open (which is where you lose the filename, btw)? Browsers are still pretty awful about this. You (probably) won't find a better solution than window.opening the download URL so the browser can get the correct headers and apply the filename to the download. Commented Mar 17, 2017 at 19:46
  • I just want to download the file without doing a postback - I have no idea how to do that with Angular2 and a post. A synchronous solution would be fine! Commented Mar 17, 2017 at 19:49
  • 1
    I don't have one, sorry. I just got working on mass downloads through a browser, and I hit my head up against this issue. Had a post that returned a file and I had to process a bunch of them. After trying a number of things, all of which sucked or failed for whatever reason on browser x or y, I converted the post call to a get call and opened the bloody thing in a new tab. It worked, and was cross-browser compatible. I'm not saying that's the only solution, but it was the only one I could find that was reliable. I still can't get clean enough. Brb, shower. Commented Mar 17, 2017 at 19:53
  • 1
    well at least I'm not the only one :P Commented Mar 17, 2017 at 20:06
  • ugh... so easy :P stackoverflow.com/questions/40240796/… Commented Mar 17, 2017 at 20:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.