首页 > Web开发 > 详细

.net core 将图片存入environment.WebRootPath 后台服务时 对于图片的访问

时间:2021-07-11 17:37:51      阅读:15      评论:0      收藏:0      [点我收藏+]

将图片存入environment.WebRootPath 后台服务时 对于图片的访问

直接后台访问子资源路径,虽然这样访问对于前端会方便很多 但是实际上这样访问会暴露相应的api接口,对于安全的上的考虑,采用base64的字符串进行显示,字符串前需要加入data的类型,便于识别,此操作也可以在前端进行展示完成,但是作为主要开发后端的人来说,还是后台进行转化方便一些,还有就是有的浏览器对子资源的访问限制,,,令人头痛

一个小例子

environment.WebRootPath :图片存的路径

后台读取方法如下:

var content = Path.Split("\\");
var datatype = "data:image/jpeg;base64,{0}";
string path = System.IO.Path.Combine(environment.WebRootPath, content[0], content[1]);
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
var s = Convert.ToBase64String(bytes);
return string.Format(datatype, s);

测试例子(不是规范的测试写法):

[TestMethod]
public void IsExistsImgPath()
{
ImgPathViewModel imgPathView = new ImgPathViewModel(environment.Object);
imgPathView.ImgName = "20210702163812rbac0.png";
imgPathView.Path = "imagesBPM\\20210702163812rbac0.png";
Assert.IsNotNull(imgPathView.PathString);
}

.net core 将图片存入environment.WebRootPath 后台服务时 对于图片的访问

原文:https://www.cnblogs.com/wd4j/p/14998558.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!