.NET核心添加静态文件夹(WWW)文件夹并保存图像文件(.Net core Add folder

2019-09-29 20:25发布

在静态文件夹添加文件和文件夹的静态保存图像。

在点网核心项目的base64保存图像。

Answer 1:

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 core Add folder in static folder (www) and save image file