internal server error 500 and file not downloaded although file creating on server ?

ahmed salah 3,216 Reputation points
2022-03-31T18:04:04.987+00:00

i work on application web api core 2.2 working with ui angular
i design page download file
file i need to download is create but can't download
although i not get any exception error

error i get
ERROR HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: 'Internal Server Error', url: 'http://localhost:61265/api/Z2Delivery/ExportNormalizedRelation/', ok: false, …}

what i try as below

on web api
[HttpPost, DisableRequestSizeLimit]
[Route("ExportNormalizedRelation")]
public IActionResult ExportNormalizedRelation()
{
return File(zipPath, "text/plain", Path.GetFileName(zipPath));

        }  

188842-final-issue-zip-download.png

error display as below

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]  
      An unhandled exception has occurred while executing the request.  
System.InvalidOperationException: No file provider has been configured to process the supplied file.  
   at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.GetFileInformation(VirtualFileResult result)  
   at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)  
   at Microsoft.AspNetCore.Mvc.VirtualFileResult.ExecuteResultAsync(ActionContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()  
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)  
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)  
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)  
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)  
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)  
warn: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[11]  
      The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the policy by listing individual origins if credentials needs to be supported.  
warn: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[11]  
      The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the policy by listing individual origins if credentials needs to be supported.  
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]  
      An unhandled exception has occurred while executing the request.  
System.InvalidOperationException: No file provider has been configured to process the supplied file.  
   at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.GetFileInformation(VirtualFileResult result)  
   at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)  
   at Microsoft.AspNetCore.Mvc.VirtualFileResult.ExecuteResultAsync(ActionContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()  
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()  
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)  
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)  
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)  
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)  
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)  

so please how to solve issue please ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,559 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Takahito Iwasa 4,851 Reputation points MVP
    2022-03-31T18:38:34.203+00:00

    Hi. @ahmed salah

    Please refer to the following SO article and try using PhysicalFileProvider.
    https://stackoverflow.com/questions/53631178/invalidoperationexception-on-file-return

    0 comments No comments

  2. AgaveJoe 28,371 Reputation points
    2022-03-31T18:45:43.95+00:00

    Change the code to...

    return File(System.IO.File.OpenRead(zipPath), "application/zip", Path.GetFileName(zipPath));
    

    This assumes you are downloading a zip file. Otherwise, if you are downloading a text file then text/plain is fine or use application/octet-stream if the file type is unknown.

    0 comments No comments

  3. Zhi Lv - MSFT 32,336 Reputation points Microsoft Vendor
    2022-04-01T02:02:41.363+00:00

    Hi @ahmed salah ,

    Agree with AgaveJoe, to download the zip file, first we need to read the file into the FileStream or a byte array, return a FileContentResult to download the file.

    So you can use the following code:

     return File(System.IO.File.OpenRead(zipPath), "application/zip", Path.GetFileName(zipPath));  
    

    Or

    return File(System.IO.File.ReadAllBytes(zipPath), "application/zip", Path.GetFileName(zipPath));  
    

    The difference between OpenRead and ReadAllBytes is that, OpenRead will read the file to FileStream and the ReadAllBytes will read the file to a byte array.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

  4. SurferOnWww 2,816 Reputation points
    2022-04-01T04:14:59.873+00:00

    Where did you put the file you want to download? If you use the File method the file must be under wwwroot and the 1st argument of the File method must be virtual path.

    For example, if you put the test.pdf under wwwroot/files the code will be as follows:

    public IActionResult DownloadFile()  
    {  
      string virtualPath = "/files/test.pdf";   
      return File(virtualPath, "application/pdf", "test.pdf");  
    }  
    

    If you put the file somewhere outside wwwroot and want to set the physical path use the PhysicalFileResult class:

    PhysicalFileResult Class
    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.physicalfileresult?view=aspnetcore-6.0

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.