
[英]Asp.Net MVC 5 FileResult always download file with ANSI encoding
[英]Angular2 file download with Asp.Net MVC is always named 'download.xlsx' - how do I change the name?
控制器方法:
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任何想法,我错过了什么? 我觉得我很遗憾......谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.