如何在 JavaScript 中检索 HttpResponseMessage 文件名
How to retrieve HttpResponseMessage FileName in JavaScript
我有这个 WebAPI 方法,它将自定义对象 MyType
作为输入并基于该对象生成 PDF。 PDF 文件作为 HttpResponseMessage
返回。请注意,我在 response.Content.Headers.ContentDisposition.FileName
:
上指定了文件名
ASP.NET WebAPI
[Route("")]
public HttpResponseMessage Get([FromUri]MyType input)
{
var pdfContent = PdfGenerator.Generate(input);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = pdfContent;
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "MyCustomFileName.pdf"
return response;
}
在 AngularJS 中,我使用 FileSaver.js 获取文件,如下所示:
$http.get("api/pdf/", {
params: {
"type": myObject
},
responseType: 'arraybuffer'
}).then(function (results) {
var data = results.data;
var file = new Blob([data], { type: 'application/pdf' });
saveAs(file, 'filename.pdf');
}, function (results) {
console.log(results);
});
它正常工作,但我在 WebAPI 和 JavaScript 中都定义了文件名。有没有办法,我可以在 JavaScript 中的 results
变量中检索 WebAPI 中定义的文件名?
$http
的方法返回的承诺作为参数传递 object,具有以下属性 (ref):
- data – {string|Object} – The response body transformed with the transform functions.
- status – {number} – HTTP status code of the response.
- headers – {function([headerName])} – Header getter function.
- config – {Object} – The configuration object that was used to generate the request.
- statusText – {string} – HTTP status text of the response.
所以results.headers('Content-Disposition')
会给你Content-Disposition
header的值。您将不得不进行一些简单的解析才能获得实际的文件名。
我有这个 WebAPI 方法,它将自定义对象 MyType
作为输入并基于该对象生成 PDF。 PDF 文件作为 HttpResponseMessage
返回。请注意,我在 response.Content.Headers.ContentDisposition.FileName
:
ASP.NET WebAPI
[Route("")]
public HttpResponseMessage Get([FromUri]MyType input)
{
var pdfContent = PdfGenerator.Generate(input);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = pdfContent;
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "MyCustomFileName.pdf"
return response;
}
在 AngularJS 中,我使用 FileSaver.js 获取文件,如下所示:
$http.get("api/pdf/", {
params: {
"type": myObject
},
responseType: 'arraybuffer'
}).then(function (results) {
var data = results.data;
var file = new Blob([data], { type: 'application/pdf' });
saveAs(file, 'filename.pdf');
}, function (results) {
console.log(results);
});
它正常工作,但我在 WebAPI 和 JavaScript 中都定义了文件名。有没有办法,我可以在 JavaScript 中的 results
变量中检索 WebAPI 中定义的文件名?
$http
的方法返回的承诺作为参数传递 object,具有以下属性 (ref):
- data – {string|Object} – The response body transformed with the transform functions.
- status – {number} – HTTP status code of the response.
- headers – {function([headerName])} – Header getter function.
- config – {Object} – The configuration object that was used to generate the request.
- statusText – {string} – HTTP status text of the response.
所以results.headers('Content-Disposition')
会给你Content-Disposition
header的值。您将不得不进行一些简单的解析才能获得实际的文件名。