控制器方法:
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方法:
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);
};
});
该文件已完美下载,但文件名始终为 download.xlsx 任何我不知道的内容?我觉得我很遗憾......谢谢!