c# - 如何从.net核心API和数据库显示图像角度
问题描述
我想显示一张图片,它的路径存储在数据库中
这就是它将图像文件传输到数据库的方式。
public string UploadImage(IFormFile file)
{
if (file == null) throw new Exception("Pusty plik");
if (file.Length == 0)
{
throw new Exception("Pusty plik");
}
if (!ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(file.FileName).ToLower())) throw new Exception("Zły typ pliku");
if (string.IsNullOrWhiteSpace(host.WebRootPath))
{
host.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}
var uploadFilesPath = Path.Combine(host.WebRootPath, "images");
if (!Directory.Exists(uploadFilesPath))
Directory.CreateDirectory(uploadFilesPath);
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
var filePath = Path.Combine(uploadFilesPath, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyToAsync(stream);
}
var path = Path.Combine(Directory.GetCurrentDirectory(), @"/wwwroot/images/", fileName);
return path;
}
解决方案
在您的示例代码中,我没有看到从数据库返回的路径。
同样从安全的角度来看,返回/wwwroot/images/
响应也是不好的做法。您可以在共享位置上创建一个文件夹并返回共享位置路径。
似乎您有疑问,并且相同的代码没有对齐。
推荐阅读
- dotnetnuke - DNN - 如何为 Bootstrap / JQuery 文件添加 CDN(云)引用?
- c# - 如何从 url 获取反序列化的 json
- python - Pandas 数据帧高效算法
- r - How to use the apply function to find the mean of array entry in the same position in R
- xml - 解析 XML 以导出到 CSV
- wpf - 如何使用 ReactiveUI MessageBus 模式在两个 UserControl 之间进行通信?
- python - 情绪一次只看一个字符
- javascript - Ooxml 中不显示下划线
- javascript - 如何获取对象中的参数
- dart - 当隐式转换关闭时,如何阻止 Dart Analyzer 抱怨 json_serializable 和 dart::convert 之间的类型不匹配?