Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
8
Indexable
public static class ManageFile
{
    public static string UploadPhoto(this IWebHostEnvironment WebHost, Microsoft.AspNetCore.Http.IFormFile file, string PathName)
    {
        string src = "";
        string root = "wwwroot/";
        if (!Directory.Exists(root + $"Images/{PathName}"))
        {
            Directory.CreateDirectory(root + $"Images/{PathName}");
        }
        if (file is not null)
        {
            src = $"Images/{PathName}/" + Guid.NewGuid() + file.FileName;
            string path = Path.Combine(WebHost.ContentRootPath, root, src);
            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fileStream);
            }
        }
        return src;
    }
    public static bool DeleteFile(string ImageUrl, IWebHostEnvironment WebHost)
    {
        string filename = Path.GetFileName(ImageUrl);
        string filePath = Path.Combine(WebHost.ContentRootPath, "wwwroot", "Images", "News", filename);

        if (File.Exists(filePath))
        {
            try
            {
                File.Delete(filePath);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}
Editor is loading...
Leave a Comment