0

$scope.newFile is my response from backend. Actually my response should be a text file, which is working in postman.But in browser , I am getting

Cannot GET /Organizer/%7B%22data%22:%22id/tname/temail/tphone/twebsite/tvenue/ttags/n83/tAny%20Name/[email protected]/t9009009009/thttp://www.anyname.com/tHall%20A/ttag1,%20tag2,%20tag3/nsunitha/[email protected]/t55555541/thttp://www.sunitha.com/nSuhasini/[email protected]/t955555544/thttp://www.suha.com/nRaichel/[email protected]/t955548458/thttp://www.raichel.com/n%22,%22status%22:200,%22config%22:%7B%22method%22:%22GET%22,%22transformRequest%22:[null],%22transformResponse%22:[null],%22jsonpCallbackParam%22:%22callback%22,%22headers%22:%7B%22Authorization%22:%22Token%2013946cc6c575d61b042b01b6905f1d239b3d9b08%22,%22Accept%22:%22application/json,%20text/plain,%20*/*%22%7D,%22url%22:%22http://http://localhost/1290//entity/campaigns/download_exhibitors/%22%7D,%22statusText%22:%22OK%22,%22xhrStatus%22:%22complete%22%7D

Service.js

var url =' http://localhost/1290/';

function downloadExhibitor() {

        var token = 129821sahh;
        var auth = "Token" + ' ' + token;

        var config = {
            headers: {
                'Content-Type': 'text/plain',
                'Authorization': auth
            }
        }

        return $http.get(url + 'entity/campaigns/download_exhibitors/', config)
               .then(successHandler, errorHandler);
}

function successHandler(response){
    /* we've got file's data from server */
    return response.data;
}

function errorHandler(error){
    /* we've got error response from server */
    throw new Error('ERROR ' + error);
}

and eventually the service invocation

JS:

$scope.newFile = "";
service.downloadExhibitor()
       .then(function(data){
                $scope.newFile = data;
             }, function(error){ 
                console.log(error);
             });

HTML:

    <button class="btn" ng-click="downloadAllExhibitors();">
<a ng-href="{{newFile}}" target="_blank">Download</a></button>
3
  • Why do you get with 'Content-Type': 'application/json' should it be 'Content-Type': 'text/plain'? Commented Apr 4, 2018 at 10:51
  • I tried that too Commented Apr 4, 2018 at 11:15
  • Have you check the restapi logs and the developer console in your browser for the http response code etc.? Commented Apr 4, 2018 at 12:30

2 Answers 2

2

You can try below code in controller...

var file = new Blob([data], {
    type : 'text/plain'
});
if (navigator.userAgent.indexOf('MSIE') !== -1
        || navigator.appVersion.indexOf('Trident/') > 0) {
        window.navigator.msSaveOrOpenBlob(file);
} else {
    var fileURL = URL.createObjectURL(file);
    window.open(fileURL);
}
Sign up to request clarification or add additional context in comments.

2 Comments

its opening as blob:127.0.0.1:50152/8830652a-9dbd-426e-bcab-4385f3512b6e with datas displaying in table , but not downloading text file.
thanks Akshaya, it worked in otherway.Between your answer was usefull.
0

Following code in controller made my work simple , and it downloaded the file finally.

            var file = new Blob([data], {
                    type: 'text/plain'
                });
                if (navigator.userAgent.indexOf('MSIE') !== -1 ||
                    navigator.appVersion.indexOf('Trident/') > 0) {
                    window.navigator.msSaveOrOpenBlob(file);
                } else {
                    var a = window.document.createElement("a");
                    a.href = window.URL.createObjectURL(file, {
                        type: "text/plain"
                    });
                    a.download = "filename.csv";
                    document.body.appendChild(a);
                    a.click(); 
                    document.body.removeChild(a);
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.