如何使用$ http从angular获取contenttype

时间:2016-02-27 20:13:56

标签: angularjs post xmlhttprequest

我希望能够在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');
                                })
                            })

                        }
                    }
                }])

0 个答案:

没有答案