我希望能够在AngularJS POST中获取ContentType,并且似乎无法在网络上的任何位置找到它。
如何使用POST中的实际ContentType替换headers部分,而不是将其硬编码为text / plain?
$http({
url: ajaxUrl,
method: 'POST',
params: { ExternalDocumentID: attrs.downloadType, FileID: attrs.downloadId },
headers: { 'Content-type': 'text/plain' },
responseType: 'arraybuffer'
.directive('fileDownload', ['$http', '$parse', function ($http, $parse)
{
return {
restrict: 'A',
link: function (scope, element, attrs, controller) {
//for testing purpose. need to be removed.
console.log('download type on file download directive: ' + attrs.downloadType)
console.log('download id on file download directive: ' + attrs.downloadId)
element.bind('click', function () {
ajaxUrl = root + 'FileUpload/download';
$http({
url: ajaxUrl,
method: 'POST',
params: { ExternalDocumentID: attrs.downloadType, FileID: attrs.downloadId },
headers: { 'Content-type': 'text/plain' },
responseType: 'arraybuffer'
})
.success(function (data, status, headers, config) {
var fileName = attrs.downloadType + "_" + attrs.downloadId;
var contentType = headers('Content-Type');
var file = new Blob([data], { type: contentType });
saveAs(file, fileName + '.txt');
})
})
}
}
}])