How to upload file from server to ng-click in angular?

I have an activity that downloads a file to my local system. The file can be jpeg pdf or word.

Now when I run this activity with ASP.Net the file is loaded. But when I call this action on ng click of the button. Neither am I getting any error, nor is the file uploading.

Snipppet Code

$scope.downloadFile = function () {
    debugger;
    $http(
        {
            url: '/ControllerName/DownloadFile',
            method: 'POST',
            data: {},
            responseType: 'arraybuffer' ,
        })
    .success(function (data, status) {
        if (status == 200) {
            var file = new Blob([data], { type: 'Application/msword' });
            saveAs(file, 'test.docx');
            alert('File downloaded');

        }
    })
    .error(function () {
        alert("Error");
    })
}

      

In this scenaio, quiiet obivious, I get saveAs Undefined error. Hence I have included filesaver.js

Now I'm not sure how to enable the fileSaver dependency with my controller. Because if I just include the following: -

app.module('myApp',['fileSaver']);

      

again i get the error.

How do I get rid of this error or is there another way to download the file?

Note. The file can be jpeg, word, pdf, zip I use tag

+3


source to share


2 answers


Angular Saver File Available. Please check if this helps.



+2


source


There is no need to inject angular injection here, just include the file directly in your project, it will work

you can include this js file to download

https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js



app.js

app.module('myApp',[]);

      

remove fileSaver from app.js

0


source







All Articles