Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
api code
public FileContentResult ExportExcel([FromRoute] string fileID)
      {
          string fileStore = Configuration.GetFolder("MasterDataStore");
          string filePath = Path.Combine(fileStore, fileID);
          var files = Directory.GetFiles(filePath, $"*{fileID}*.xls");

          base.HttpContext.Response.Headers.Add("content-disposition", "attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls");
          this.Response.ContentType = "application/vnd.ms-excel";

          return File(System.IO.File.ReadAllBytes(files[0]), "application/vnd.ms-excel", $"{fileID}.xls");
      }


When I directly hitting api , i am able to download the proper xls file , but when I am hitting it through angular js code to download the file , the downloaded file is in wrong format.

Angular js code

$scope.download = function (fileName) {
           debugger
           if (fileName != null && fileName != '' && fileName != undefined) {
               URIService.GetData(URIService.GetDownloadExcelFileUrl(fileName))
                   .success(function (data) {
                       var file = new Blob([data], {
                           type: 'application/vnd.ms-excel'
                       });
                       FileSaver.saveAs(file, fileName, true);


What I have tried:

$scope.download = function (fileName) {
           debugger
           if (fileName != null && fileName != '' && fileName != undefined) {
               URIService.GetData(URIService.GetDownloadExcelFileUrl(fileName))
                   .success(function (data) {
                       var file = new Blob([data], {
                           type: 'application/vnd.ms-excel'
                       });
                       FileSaver.saveAs(file, fileName, true);


tried the above code, but i am getting downloaded file in corrupt format.
Posted
Updated 20-Sep-22 22:39pm
v3
Comments
Richard MacCutchan 20-Sep-22 3:38am    
As with your question yesterday you have not explained what is wrong with the file content.
Himansh jain 20-Sep-22 6:34am    
https://drive.google.com/file/d/1ROFq9mj2AqsHd7u2j6KESBTn5W24zj3p/view?usp=sharing

Please find the screenshot of the file when I try to open it.
Richard MacCutchan 20-Sep-22 7:27am    
I told you yesterday that Excel files are not clear text. They are binary files that are in a special format used by Excel.
Himansh jain 20-Sep-22 7:29am    
But Through directly hitting the API I am able to download the xls file correctly.
Only when I am hitting API through the angularjs code, i am seeing this. So the issue must be with angularjs code.
Richard MacCutchan 20-Sep-22 7:36am    
No the issue is that you are trying to read the file as clear text. The only way to check the content is to open it with Excel, or another app that can decode .xls files.

1 solution

[HttpGet]
        [Route("Excel/{fileID}")]
        public IActionResult ExportExcel([FromRoute] string fileID)
        {
            try
            {
                string fileStore = Configuration.GetFolder("MasterDataStore");
                var files = Directory.GetFiles(fileStore, $"*{fileID}*.xls");

                base.HttpContext.Response.Headers.Add("content-disposition", "attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls");
                this.Response.ContentType = "application/octet-stream";

                return File(System.IO.File.ReadAllBytes(files[0]), "application/octet-stream", $"{fileID}.xls");
            }
            catch(Exception ex)
            {
               
                return NotFound();
            }
        }




UI

$scope.download = function (fileName) {
            $scope.showAlertData = false;
                $scope.alertData = {};
            if (fileName != null && fileName != '' && fileName != undefined) {
                URIService.GetDataExcel(URIService.GetDownloadExcelFileUrl(fileName))
                    .success(function (data) {
                        var file = new Blob([data], {
                            type: 'application/octet-stream'
                        });                  
                        FileSaver.saveAs(file, fileName+".xls", true);
                    }).error(function (data, status, headers, config) {
                       
                        var errorMessage = "";
                        if (status == "404" || data == undefined) {
                            errorMessage = "Menu.FileNotFound"
                        }
                        else {
                            errorMessage = "Menu.Error_Message"
                        }
                    });
            }

        }



this.GetDataExcel = function (url) {
    var response = $http({
        method: 'GET',
        url: url,
        headers: { 'Content-Type': "application/octet-stream" },
        responseType: 'blob'
    })

    var returnValue = response;
    response.success(function (data, status, headers, config) {

    }).error(function (data, status, headers, config) {
        OnError(data, status, headers, config);
    });

    return returnValue;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900