Search code examples
c#asp.netasp.net-mvcasp.net-core.net-core

InvalidOperationException on File return


am running into some weird issue when i try to return a file to be downloaded, so this is my code

string filePath = Path.Combine(Path1, Path2, filename);
return File(filePath, "audio/mp3", "myfile.mp3");

but the problem it return this error

InvalidOperationException: No file provider has been configured to process the supplied file.

am not sure what i have missed, any help ?


Solution

  • so the way to return a File method , is as @SeM suggest but by removing the file name from file path.

    string filePath = Path.Combine(Path1, Path2);
    
    IFileProvider provider = new PhysicalFileProvider(filePath);
    IFileInfo fileInfo = provider.GetFileInfo(filename);
    var readStream = fileInfo.CreateReadStream();
    
    return File(readStream, "audio/mpeg", fileName);