1

MVC Part

return File(stream, "application/vnd.ms-excel", file.FileName);

I'm returning file from MVC controller in above way.

AngularJS controller

$scope.ExcelValidate = function () {
    if ($scope.files && $scope.files.length && $scope.selectedFileType != -1) {
        busyIndicatorService.showBusy("Uploading data...");
        for (var i = 0; i < $scope.files.length; i++) {
            var file = $scope.files[i];
            $scope.file = file.name;
            Upload.upload({
                url: '/MasterExcelImport/ValidateExcelData',
                fields: {
                    uploadType: $scope.selectedFileType.Key.Key
                },
                file: file
            }).progress(function (evt) {
                console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
                $scope.file.progress = parseInt(100.0 * evt.loaded / evt.total);
            }).success(function (data) {
                busyIndicatorService.stopBusy();
                var blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                var objectUrl = URL.createObjectURL(blob);
                window.open(objectUrl);
            }).error(function (data, status, headers, config) {
                busyIndicatorService.stopBusy();
            });
        }
    }
};

Return file already comes to the success section. But downloading part got failed.

Could someone help me to resolve this issue please?

Thanks, Erandika Sandaruwan

2
  • are you getting base64 string as a result of http call Commented Feb 23, 2017 at 5:14
  • What error message are you getting?
    – georgeawg
    Commented Feb 23, 2017 at 8:05

1 Answer 1

1

Mvc/WeApi Controller Action

 [HttpPost]
    public HttpResponseMessage DownloadExcelFile()
    {
        var stream = new MemoryStream();//fill stream from data or use filestream
        var result = Request.CreateResponse(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(stream.ToArray());
        result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = "SystemPerformance.xlsx";
        return result;
    }

Angular Contoller method

 ExampleService.DownloadExcelFile({}/*post data*/,/*success function*/ function (response) {
        var headers = response.headers();
        var blob = new Blob([response.data], { type: headers['content-type'] });
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = "excelfile.xlsx";
        link.click();
    }, /*error function*/ function (err) {

    });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.