.Net核心在静态文件夹(www)中添加文件夹并保存图像文件 [英] .Net core Add folder in static folder (www) and save image file

查看:121
本文介绍了.Net核心在静态文件夹(www)中添加文件夹并保存图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在静态文件夹中添加文件,然后将图像保存在静态文件夹中。

Add file in the static folder and save an image in the static folder.

将base64图像保存在Dot net核心项目中。

Save base64 image in the Dot net core project.

推荐答案

public class EventMastersController : Controller
{
    private IHostingEnvironment _env; 

    public EventMastersController(IHostingEnvironment env)
    {
        _env = env;
    }

    public void AddFolderAndImage()
    {
        var webRoot = _env.WebRootPath;
        var PathWithFolderName = System.IO.Path.Combine(webRoot, "MyFolder");


        if (!Directory.Exists(PathWithFolderName))
        {
            // Try to create the directory.
            DirectoryInfo di = Directory.CreateDirectory(PathWithFolderName);


            string Base64String = eventMaster.BannerImage.Replace("data:image/png;base64,", "");

            byte[] bytes = Convert.FromBase64String(Base64String);

            Image image;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                image = Image.FromStream(ms);
            }

            image.Save(PathWithFolderName + "/ImageName.png");
    }

}

这篇关于.Net核心在静态文件夹(www)中添加文件夹并保存图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆