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!

naspinski
  • 34,020
  • 36
  • 111
  • 167
  • 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.open`ing the download URL so the browser can get the correct headers and apply the filename to the download. –  Mar 17 '17 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! – naspinski Mar 17 '17 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. –  Mar 17 '17 at 19:53
  • 1
    well at least I'm not the only one :P – naspinski Mar 17 '17 at 20:06
  • ugh... so easy :P http://stackoverflow.com/questions/40240796/angular-2-best-approach-to-use-filesaver-js – naspinski Mar 17 '17 at 20:56
  • from here: http://stackoverflow.com/questions/40240796/angular-2-best-approach-to-use-filesaver-js – naspinski Mar 17 '17 at 20:57

0 Answers0