排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256
50010702506256
欢迎加群交流技术
分类:
.NET Core
客户端
前台
<div>
<input type="file" onchange="selectImage(this.files)" accept="">
</div>
<script>
function selectImage(files) {
let reader = new FileReader();
let file = files[0];
console.log(file);
//取出来文件的后缀名(方便在后台保存的时候用)
var index = file.name.lastIndexOf(".");
var fileExt = file.name.substr(index + 1);
console.log(fileExt);
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
$.post('/Home/UpLoadFile', { filecontent: reader.result, fileExt: fileExt }, function (result) {
});
}
}
</script>客户端调用gRPC:
/// <summary>
/// 调用gRPC base64图片上传
/// </summary>
public void UpLoadFile()
{
string base64 = Request.Form["filecontent"];
string fileExt = Request.Form["fileExt"];
#region base64需要把前面的申明去掉(当然前缀这个也可以放在前端处理)
//string newbase = base64.Replace("data:image/jpeg;base64,", "");
//这里用了一下正则表达式因为可能是格式data:image/jpeg;base64,data:image/png;base64等等
string base64img = Regex.Replace(base64, "data:image/.*;base64,", "");
//文本格式(注意不同格式可能需要你针对性的写不同的格式去去掉base64前面的内容,不然会报格式不对的错)
string base64img = Regex.Replace(base64, "data:text/.*;base64,", "");
#endregion
//设置允许不安全的HTTP2支持
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var channel = GrpcChannel.ForAddress("http://localhost:8099/");
var client = new UpImg.UpImgClient(channel);
var reply = client.DoUpLoad(new UpImgRequest { Img = base64img, FileExt = fileExt });
string ss = reply.Message;
}gRPC服务端
proto:
syntax = "proto3";
option csharp_namespace = "GrpcService1";
package UpImg;
// The greeting service definition.
service UpImg {
// Sends a greeting
rpc DoUpLoad (UpImgRequest) returns (UpImgReply);
}
// The request message containing the user's name.
message UpImgRequest {
string img=1;
string fileExt=2;
}
// The response message containing the greetings.
message UpImgReply {
string message = 1;
}实现服务:
public class UpImgService : UpImg.UpImgBase
{
private readonly ILogger<GreeterService> _logger;
public UpImgService(ILogger<GreeterService> logger)
{
_logger = logger;
}
public override Task<UpImgReply> DoUpLoad(UpImgRequest request, ServerCallContext context)
{
string base64img = request.Img;
string fileExt = request.FileExt;
//把图片存储到c盘,具体操作的时候位置可以自行改变
Base64Convert.Base64ToFileAndSave(base64img, "c://" + Guid.NewGuid().ToString().Replace("-", "") +"."+ fileExt);
return Task.FromResult(new UpImgReply
{
Message = "上传成功!"
});
}
}base64工具类:
public class Base64Convert
{
/// <summary>
/// 文件转换成Base64字符串
/// </summary>
/// <param name="fileName">文件绝对路径</param>
/// <returns></returns>
public static String FileToBase64(string fileName)
{
string strRet = null;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
strRet = Convert.ToBase64String(bt);
fs.Close();
}
catch (Exception ex)
{
throw ex;
}
return strRet;
}
/// <summary>
/// Base64字符串转换成文件
/// </summary>
/// <param name="strInput">base64字符串</param>
/// <param name="fileName">保存文件的绝对路径</param>
/// <returns></returns>
public static bool Base64ToFileAndSave(string strInput, string fileName)
{
bool bTrue = false;
try
{
byte[] buffer = Convert.FromBase64String(strInput);
FileStream fs = new FileStream(fileName, FileMode.CreateNew);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
bTrue = true;
}
catch (Exception ex)
{
throw ex;
}
return bTrue;
}
}下载地址:http://www.tnblog.net/resource/show/aojiancc2/af3134fbbe174025ad34e7ccf7de465f
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价