using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; namespace MyPorject.MVC.ViewComponents
{
public class ImgToBase64ViewComponent : ViewComponent
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IMemoryCache _memoryCache;
public ImgToBase64ViewComponent(IHostingEnvironment hostingEnvironment, IMemoryCache memoryCache)
{
_hostingEnvironment = hostingEnvironment;
_memoryCache = memoryCache;
} public async Task InvokeAsync(string src)
{
string cacheKey = src;
string result = await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
{
string webRootPath = _hostingEnvironment.WebRootPath;
string path = webRootPath + @"\" + src.TrimStart('~').TrimStart('/').Replace(@"/", @"\").ToString(); try
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var byteArray = new byte[fs.Length];
await fs.ReadAsync(byteArray, 0, byteArray.Length);
result = "data:image/jpeg;base64," + Convert.ToBase64String(byteArray);
}
}
catch (Exception ex)
{
result = ex.Message;
}
return await Task.FromResult(result);
});
return View("", result);
}
}
}

添加default.cshtml

使用方法:

将原来src中改成Component.InvokeAsync 就可以使用了

效果如图:

.net core使用ViewComponent将页面图片转码成base64的更多相关文章

  1. js如何将选中图片文件转换成Base64字符串?

    如何将input type="file"选中的文件转换成Base64的字符串呢? 1.首先了解一下为什么要把图片文件转换成Base64的字符串 在常规的web开发过程中,大部分上传 ...

  2. js 将图片文件转换成base64

      1.情景展示 在JavaScript中,如何使用图片文件转换成base64? 2.解决方案 /** * 网络图像文件转Base64 * @param img dom对象 */ function g ...

  3. 图片文件转换成Base64编码实现ajax提交图片

    //上传头像图片 function uploadHead(imgPath) { console.log("imgPath = " + imgPath); var image = n ...

  4. php 将图片文件转成base64编码的方法

    php 将图片文件转成base64编码的方法<pre><?php /** 文件转base64输出 * @param String $file 文件路径 * @return Strin ...

  5. 将input type="file" 类型的图片文件转成base64

    带有图片的form表单上传数据是很麻烦的,因为图片通常都是和文字分开上传,这是很麻烦的,所有吧图片转成base64就可以和当成文字上传了.话不多少,看代码: 首先定义一个类型为file的input标签 ...

  6. C++读写图片数据转成Base64格式

    转载:http://www.cnblogs.com/jeray/p/8746976.html 转载:https://www.cnblogs.com/lujin49/p/4957742.html 转载: ...

  7. C++读写图片数据转成Base64格式的一种方法

    最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了C++语言读写图片转化成Base64格式进行传输.具体代码如下: //++Base64.h #pragma once class C ...

  8. 图片链接转成base64

    一半需要我的图像转换为base64字符串,这样我们可以把我的形象到服务器.现在我们提供一个js: function convertImgToBase64(url, callback, outputFo ...

  9. 将图片文件转成BASE64格式

    html5Reader (file, item) { const reader = new FileReader() reader.onload = (e) => { this.$set(ite ...

随机推荐

  1. 动态sql语句,非存储过程,如何判断某条数据是否存在,如果不存在就添加一条

    已知一个表 table 里面有两个字段  A1 和 A2 如何用动态语句 判断 A1 = A , A2=B 的数据是否存在,如果不存在,就添加一条数据, A1 = A , A2 = B INSERT  ...

  2. 3.ifconfig

    Windows下查看IP地址用ipconfig Linux 下查看IP地址用ifconfig 还有 ip addr      而ipconfig 和ip addr的区别则是与net-tools工具和i ...

  3. Winform 窗体获得焦点

    给窗体添加Shown事件 public void Form_Shown(object sender, EventArgs e) { this.Activate(); this.Focus(); //定 ...

  4. django 利用pillow 进行简单的设置验证码(python)

    1.导入模块 并定义一个验证状态 from PIL import Image, ImageDraw, ImageFont from django.utils.six import BytesIO de ...

  5. C# 使用NPOI 处理Excel(Datable与Excel相互转换)

    VS上有自带的程序集可以读取,但是总是会出现这样或那样的问题,让人恨得牙疼!而且效率太慢了.用NPOI就好多了,比较快,而且稳定,还简单,引用相应的程序集就好了. Excel转换成Datable pr ...

  6. Linux常用服务器搭建

    1.Linux常用服务器构建-ftp服务器 ftp服务器 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”. 用于Internet上的控制文件 ...

  7. JAVA小白开发环境配置(编译器为Idea)

    JDK配置 1.首先到官网下载最新版JDK:Oracle官网下载 Accept License Agreement–>下载适合自己pc版本的jdk(此处以64位windows为例.x86是32位 ...

  8. [Swift]LeetCode224. 基本计算器 | Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  9. [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  10. [Swift]LeetCode880. 索引处的解码字符串 | Decoded String at Index

    An encoded string S is given.  To find and write the decodedstring to a tape, the encoded string is ...