现在有一个api, 提供图片的下载,如下代码,,调试出现
InvalidOperationException: No file provider has been configured to process the supplied file. 的错误。
var rootPath = _hostingEnvironment.ContentRootPath; var photoName= "photo.jpeg"; bitmap.Save($@"{rootPath}\{photoName}", ImageFormat.Jpeg); // 变量bitmap 为Bitmap实例 return File(rootPath, "image/jpeg", photoName);
经过查找可以这样写,如下代码,这样就返回解决了问题。
var rootPath = _hostingEnvironment.ContentRootPath; var photoName= "photo.jpeg"; bitmap.Save($@"{rootPath}\{photoName}", ImageFormat.Jpeg);
// 一种方式 var provider = new PhysicalFileProvider(rootPath); // 或者这样写 var provider = _hostingEnvironment.ContentRootFileProvider; var fileInfo = provider.GetFileInfo(photoName); var readStream = fileInfo.CreateReadStream();
// 另一种更直接的方式
// var readStream = System.IO.File.ReadAllBytes($@"{rootPath}\{photoName}"); return File(readStream, "image/jpeg", photoName);
参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/file-providers?view=aspnetcore-2.2