如何在 Typescript 中解析 C# FileContentResult
How to parse C# FileContentResult in Typescript
我正在开发一个 Angular 2 应用程序,我们允许用户下载 Word 文档。现在它正在工作,但返回的名称是一个长 string/GUID。我需要下载文件的实际名称。这是我们的设置:
在客户端:
downloadWordTemplate(): void {
this._data.getDocument(this.document.docKey)
.subscribe(data => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
const url = window.URL.createObjectURL(blob);
const winObj = window.open(url);
if (!winObj || winObj.closed || typeof winObj.closed === 'undefined') {
this._toastr.info('File download blocked by pop-up blocker');
}
});
}
服务电话:
getDocument(docKey: string): Observable<any> {
return this._http.get(`${this._env.systemApi}/Document/GetDocument/${docKey}`, { responseType: 'arraybuffer' });
}
后端调用:
public async Task<IActionResult> GetDocument(string docKey)
{
var document= await Task.Run(() => _context.Documents
.Where(x => x.key== docKey)
.Select(x => new
{
AttachmentName = x.WordAttachmentName,
MimeDataType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
AttachmentBinary = x.WordAttachmentBinary
}).FirstOrDefault());
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = document.AttachmentName,
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");
return File(document.AttachmentBinary,
document.MimeDataType,
document.AttachmentName);
}
所以在控制台中,我可以看到当数据返回到订阅方法时有三个数组,我认为它们对应于 AttachmentBinary、MimeDataType 和 AttachmentName,但我不确定如何解析出名字。
下面是 return 数据在开发工具中的样子:
我继承了这个,之前我没有使用过 Blob 数据,所以有点卡住了。
您可以在响应请求中使用文件名设置header。
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", FileName));
我弄明白了,需要补充:
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
到后端代码。
我正在开发一个 Angular 2 应用程序,我们允许用户下载 Word 文档。现在它正在工作,但返回的名称是一个长 string/GUID。我需要下载文件的实际名称。这是我们的设置:
在客户端:
downloadWordTemplate(): void {
this._data.getDocument(this.document.docKey)
.subscribe(data => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
const url = window.URL.createObjectURL(blob);
const winObj = window.open(url);
if (!winObj || winObj.closed || typeof winObj.closed === 'undefined') {
this._toastr.info('File download blocked by pop-up blocker');
}
});
}
服务电话:
getDocument(docKey: string): Observable<any> {
return this._http.get(`${this._env.systemApi}/Document/GetDocument/${docKey}`, { responseType: 'arraybuffer' });
}
后端调用:
public async Task<IActionResult> GetDocument(string docKey)
{
var document= await Task.Run(() => _context.Documents
.Where(x => x.key== docKey)
.Select(x => new
{
AttachmentName = x.WordAttachmentName,
MimeDataType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
AttachmentBinary = x.WordAttachmentBinary
}).FirstOrDefault());
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = document.AttachmentName,
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");
return File(document.AttachmentBinary,
document.MimeDataType,
document.AttachmentName);
}
所以在控制台中,我可以看到当数据返回到订阅方法时有三个数组,我认为它们对应于 AttachmentBinary、MimeDataType 和 AttachmentName,但我不确定如何解析出名字。
下面是 return 数据在开发工具中的样子:
我继承了这个,之前我没有使用过 Blob 数据,所以有点卡住了。
您可以在响应请求中使用文件名设置header。
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", FileName));
我弄明白了,需要补充:
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
到后端代码。