angularjs - Saving a file using the ContentType with a valid file extension -
i want able save file via http post valid file extension.
i can't use contenttype because it's not valid file extension.
i'm trying angularsjs http post.
note last line in code. how can give file proper extension instead of hard coding it?
.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'); })
create conversion function:
function extconvert(contenttype) { var exthash = { "text/plain": "txt", "application/json": "json" } return exthash[contenttype] || "txt"; };
then use in code:
httppromise.then(function onfulfilled(response) { var filename = attrs.downloadtype + "_" + attrs.downloadid; var contenttype = response.headers('content-type'); var file = new blob([response.data], { type: contenttype }); var ext = extconvert(contenttype); filesaver.saveas(file, filename + '.' + ext'); });
additional resources
deprecation notice
the
$http
legacy promise methods.success
,.error
have been deprecated. use standard.then
method instead.1
Comments
Post a Comment