.Net core 在静态文件夹(www)中添加文件夹并保存图片文件

.Net core Add folder in static folder (www) and save image file

在静态文件夹中添加文件并在静态文件夹中保存图像。

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

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");
    }

}