admin管理员组文章数量:1289653
I've this WebAPI method, that takes a custom object MyType
as input and generate a PDF based on that. The PDF-file is returned as a HttpResponseMessage
. Note that I specify the filename on 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;
}
In AngularJS I fetch the file using FileSaver.js like this:
$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);
});
It works as excepted, but I'm defining the filename both on WebAPI and in the JavaScript. Is there a way, that I can retrieve the FileName defined in WebAPI in the results
variable in JavaScript?
I've this WebAPI method, that takes a custom object MyType
as input and generate a PDF based on that. The PDF-file is returned as a HttpResponseMessage
. Note that I specify the filename on 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;
}
In AngularJS I fetch the file using FileSaver.js like this:
$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);
});
It works as excepted, but I'm defining the filename both on WebAPI and in the JavaScript. Is there a way, that I can retrieve the FileName defined in WebAPI in the results
variable in JavaScript?
1 Answer
Reset to default 6The promise returned by methods of $http
are passed as argument an object with the following properties (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.
So results.headers('Content-Disposition')
will gives you the value of the Content-Disposition
header. You will have to do some trivial parsing to get to the actual filename.
本文标签: angularjsHow to retrieve HttpResponseMessage FileName in JavaScriptStack Overflow
版权声明:本文标题:angularjs - How to retrieve HttpResponseMessage FileName in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741467440a2380396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论