0

I am not able to download file result in js. I am returning File Result to the js but dont know how to download it

To be short and specific i am returning word document from controller to js in which i need to download it and handle it in js.

My js method in which i am calling method and file result is returning to.

vm.establishmentAllRecord = function (page) {
            if (page != undefined) {
                vm.page = page;
            }
            var searchCriteria = {
                From: vm.From,
                To: vm.To,
                Region: vm.SelectedRegion
            }
surveyService.establishmentAllRecord(searchCriteria, (vm.page * vm.pagesize), vm.pagesize, vm.sortBy, vm.sortingDirection).then(function (d) {
                var result = JSON.parse(d.data.data);
                ???
            });

I need to ask how can i handle return File Result from controller on ??? this place and download

Hopes for your suggestions

EDITED:

i have return file system from controller is in this way,

 FileResult result1 = PrintSurveyDetailsReport(VisitId); 
return result1; 

result1 contains 'ContentType="application/octet-stream"' , "FileContents" and "FileDownloadName" 
Doc
  • 179
  • 1
  • 18

1 Answers1

0

Use the following function

 var downloadFile = function (data, fileName, contentType) {
    contentType = contentType || "application/octet-stream";
    var blobObject = new Blob([data], {type: contentType});
    try {
        window.navigator.msSaveOrOpenBlob(blobObject, fileName);
    }
    catch (exp) {
        var link = document.createElement('a');
        link.setAttribute('href', URL.createObjectURL(blobObject));
        link.setAttribute('download', fileName);
        document.body.appendChild(link); // Required for FF
        link.click();
        document.body.removeChild(link);
    }
};
Wasef Anabtawi
  • 1,011
  • 1
  • 10
  • 17
  • i have return file system from controller is in this way, 'FileResult result1 = PrintSurveyDetailsReport(VisitId); return result1;' and result1 contains 'ContentType="application/octet-stream"' , "FileContents" and "FileDownloadName" how to use it in your method ? I am new to MVC – Doc Apr 02 '19 at 05:49
  • downloadFile(result.FileContents, result.FileDownloadName); – Wasef Anabtawi Apr 02 '19 at 06:33
  • @wasif there are three parameter in downloadFile function ? – Doc Apr 02 '19 at 07:34
  • it is optional , and since your content-type in the file is "application/octet-stream" , you don't need to pass it since this is the default value , did it work ? if yes kindly upvote and mark it as correct. Thank you. – Wasef Anabtawi Apr 02 '19 at 07:42
  • @wasif i get it like Data.result.FileContents. In function in angular i have Data and it is showing me File Contents and File Download but getting this error: Cannot read property 'FileContents' of undefined – Doc Apr 02 '19 at 07:52
  • try this downloadFile(Data.result, "testName.txt"); – Wasef Anabtawi Apr 02 '19 at 08:00
  • i tried downloadFile(res.result, "testName.txt"); changing angular variable to res and made same return variable from controller that is res it download file but when i open it shows Undefined written in it ? – Doc Apr 02 '19 at 08:14
  • don't parse to json , just do this downloadFile(d.data.data, "testName.txt"); if this does not work please console.log(d.data.data) and add your result to the question – Wasef Anabtawi Apr 02 '19 at 08:26
  • now i am using res intead of d so it means it will be like res.data.data ? – Doc Apr 02 '19 at 08:53
  • yes, just and console.log(res) just to make sure that you are on the right track – Wasef Anabtawi Apr 02 '19 at 09:14
  • now file is downloading with content but it need to be deseralized i tried to edit my post and show data here but it did not allow me to do so. – Doc Apr 02 '19 at 11:26
  • So, I've answered your question, kindly mark my answer as correct and kindly open an new question for the deserialization – Wasef Anabtawi Apr 02 '19 at 12:07