ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 277.9k

How to download Excel file to download path and open it on browser on

Nov 20 2020 4:20 PM

I work on asp.net core 2.2 Web Api I face issue I can't see Downloaded file on download Path and I can't see it After download on browser

download file success on path i give it but not make download on download path and I can't see it to open it directly

my code I use it for download is :

  1. [HttpPost, DisableRequestSizeLimit]  
  2.         public IActionResult Upload()  
  3.         {  
  4.             try  
  5.             {  
  6.                 var DisplayFileName = Request.Form.Files[0];  
  7.                 string fileName = DisplayFileName.FileName.Replace(".xlsx""-") + Guid.NewGuid().ToString() + ".xlsx";  
  8.                 string exportPath = myValue2 + "\\" + fileName;  
  9.              
  10.                         if (exportPath != "")  
  11.                         {  
  12.                            ex.DownloadOutPut(exportPath);  
  13.                         }  
  14.   
  15.                           
  16.                     }  
  17.                     return Ok(new { dbPath });  
  18.                 }  
  19.                 else  
  20.                 {  
  21.                     return BadRequest();  
  22.                 }  
  23.             }  
  24.             catch (Exception ex)  
  25.             {  
  26.                 return StatusCode(500, $"Internal server error: {ex}");  
  27.             }  
  28.         }  
  29. function for download I use it  
  30.     public FileStreamResult DownloadOutPut(string path)  
  31.         {  
  32.               
  33.   
  34.                 var filename = System.IO.Path.GetFileName(path);  
  35.                 var mimeType = "application/octet-stream";  
  36.                 var stream = System.IO.File.OpenRead(path);  
  37.               
  38.   
  39.             return new FileStreamResult(stream, mimeType) { FileDownloadName = filename };  
  40.         }  

result I need file as on image :





Answers (4)