.net core使用ViewComponent将页面图片转码成base64


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的更多相关文章
- js如何将选中图片文件转换成Base64字符串?
如何将input type="file"选中的文件转换成Base64的字符串呢? 1.首先了解一下为什么要把图片文件转换成Base64的字符串 在常规的web开发过程中,大部分上传 ...
- js 将图片文件转换成base64
1.情景展示 在JavaScript中,如何使用图片文件转换成base64? 2.解决方案 /** * 网络图像文件转Base64 * @param img dom对象 */ function g ...
- 图片文件转换成Base64编码实现ajax提交图片
//上传头像图片 function uploadHead(imgPath) { console.log("imgPath = " + imgPath); var image = n ...
- php 将图片文件转成base64编码的方法
php 将图片文件转成base64编码的方法<pre><?php /** 文件转base64输出 * @param String $file 文件路径 * @return Strin ...
- 将input type="file" 类型的图片文件转成base64
带有图片的form表单上传数据是很麻烦的,因为图片通常都是和文字分开上传,这是很麻烦的,所有吧图片转成base64就可以和当成文字上传了.话不多少,看代码: 首先定义一个类型为file的input标签 ...
- C++读写图片数据转成Base64格式
转载:http://www.cnblogs.com/jeray/p/8746976.html 转载:https://www.cnblogs.com/lujin49/p/4957742.html 转载: ...
- C++读写图片数据转成Base64格式的一种方法
最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了C++语言读写图片转化成Base64格式进行传输.具体代码如下: //++Base64.h #pragma once class C ...
- 图片链接转成base64
一半需要我的图像转换为base64字符串,这样我们可以把我的形象到服务器.现在我们提供一个js: function convertImgToBase64(url, callback, outputFo ...
- 将图片文件转成BASE64格式
html5Reader (file, item) { const reader = new FileReader() reader.onload = (e) => { this.$set(ite ...
随机推荐
- [LeetCode] Bus Routes 公交线路
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- 神经网络_线性神经网络 1 (Nerual Network_Linear Nerual Network 1)
2019-04-08 16:59:23 1 学习规则(Learning Rule) 1.1 赫布学习规则(Hebb Learning Rule) 1949年,Hebb提出了关于神经网络学习机理的“突触 ...
- Linux常用服务器搭建
1.Linux常用服务器构建-ftp服务器 ftp服务器 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”. 用于Internet上的控制文件 ...
- Socket看法
Socket通常也称做”套接字“,用于描述IP地址和端口,废话不多说,它就是网络通信过程中端点的抽象表示. Socket又称"套接字",应用程序通常通过"套接字" ...
- 音视频编解码技术(一):MPEG-4/H.264 AVC 编解码标准
一.H264 概述 H.264,通常也被称之为H.264/AVC(或者H.264/MPEG-4 AVC或MPEG-4/H.264 AVC) 1. H.264视频编解码的意义 H.264的出现就是为了创 ...
- [Swift]LeetCode299. 猜数字游戏 | Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- CentOS随笔——Service与防火墙关闭
Service后台服务管理 基本语法 service 服务名 start 开启服务 service 服务名 stop 关闭服务 service 服务名 restart 重启服务 service 服务名 ...
- vue项目安装vux
本文章默认基于“vue init webpack myproject”已经搭好基本的项目, 而且本文是从我有道笔记拷贝稍加修改过来的 本来我私人笔记写给自己看的所以有些地方可能描述不够清晰 需要修改的 ...
- 说一说MVC的过滤器(一)
在MVC项目中过滤器,最好把这些过滤器类放到一个文件夹中(Filters),然后过滤器文件的名称也是有规定的,格式应该为xxxAttribute,否则在控制器或控制器的方法中是无法进行调用过滤器的, ...