Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 313
Jake Weary Avatar asked Dec 05 '18 11:12

Jake Weary


People also ask

When to use InvalidOperationException?

Remarks. InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. Typically, it is thrown when the state of an object cannot support the method call.

How to handle System InvalidOperationException in c#?

You should use SingleOrDefault() instead of Single() , then you'll get null if the data isn't in the database.


4 Answers

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);
like image 169
Jake Weary Avatar answered Nov 07 '22 13:11

Jake Weary


You can simply change your code from:

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

to this:

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

Then problem solved!

You can also put your files under wwwroot folder (your web root folder). Then you can use relative path to the web root folder and put filePath into first argument of the File method. Then you can access the file without problem. This will be much safer than using PhysicalFile method.

like image 45
Will Huang Avatar answered Nov 07 '22 13:11

Will Huang


you first need to create file provider registration.

services.AddSingleton<IFileProvider>(  
       new PhysicalFileProvider(Directory.GetCurrentDirectory()));  

then you can use it like this

public class IndexModel : PageModel
{
    private readonly IFileProvider _fileProvider;

    public IndexModel(IFileProvider fileProvider)
    {
        _fileProvider = fileProvider;
    }

    public IFileInfo FileInfo { get; private set; }

    public void OnGet()
    {
        IFileInfo = _fileProvider.GetFileInfo("filename.ext");
    }
}   

in your case your function body would be like

string filePath = Path.Combine(Path1, Path2, filename);
IFileInfo = _fileProvider.GetFileInfo(filepath);
var fs = fileInfo.CreateReadStream();
return File(fs, "audio/mp3", "myfile.mp3");
like image 24
Derviş Kayımbaşıoğlu Avatar answered Nov 07 '22 11:11

Derviş Kayımbaşıoğlu


In asp.net core you need to PhysicalFileProvider to access physical file system:

string filePath = Path.Combine(Path1, Path2, filename);

IFileProvider provider = new PhysicalFileProvider(filePath);
IFileInfo fileInfo = provider.GetFileInfo(filename);
var readStream = fileInfo.CreateReadStream();

return File(readStream, "audio/mpeg", fileName);

Also as far as I know, mime type of .mp3 file is audio/mpeg.

like image 37
SᴇM Avatar answered Nov 07 '22 11:11

SᴇM