如何从我的数据库的文件夹中删除附加到记录的图像?

时间:2018-08-30 13:53:33

标签: c# asp.net asp.net-mvc delete-file system.io.file

当我从数据库中删除记录时,除了文件夹中的图像文件之外,所有内容都将被删除。

在“保存操作”中,我有运行良好的上载方法。 DeleteConfirmed操作仅适用于删除记录而不适用于图像文件,当我按下Delete按钮时,将在我的视图中向您显示拒绝访问。如果可以的话,该项目可以在IIS Express上运行。 还有其他方法可以删除记录中附加的特定文件吗?

请注意,图像存储在BlogArticle类中的ArticleImage字符串上。

这是我的控制器,负责处理保存记录的操作。

控制器

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SkylineNew.Models;
using SkylineNew.ViewModel;
using BlogArticle = SkylineNew.Models.BlogArticle;

namespace SkylineNew.Controllers
{
    public class BlogArticleController : Controller
    {
        private SkylineNewDbContext _context;

        public BlogArticleController()
        {
            _context = new SkylineNewDbContext();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }

        [Authorize(Roles = RoleName.CanManageEverything)]
        public ViewResult New()
        {
            var tags = _context.Tags.ToList();

            var viewModel = new BlogArticleViewModel
            {
                BlogArticles = new BlogArticle(),
                Tags = tags
            };

            return View("BlogArticleForm", viewModel);
        }

        public ViewResult Blog()
        {
            if (User.IsInRole("CanManageEverything"))
            {
                var blg = _context.BlogArticles.Include(b => b.Tags).ToList();

                return View("Blog", blg);
            }
            else
            {
                var blg = _context.BlogArticles.Include(b => b.Tags).ToList();

                return View("BlogReadOnly", blg);
            }
        }

        public ActionResult Details(int Id)
        {
            var blg = _context.BlogArticles.Include(b => b.Tags).SingleOrDefault(b => b.Id == Id);

            if (blg == null)
                return HttpNotFound();

            return View(blg);

        }

        [Authorize(Roles = RoleName.CanManageEverything)]
        public ActionResult Edit(int id)
        {
            var blgs = _context.BlogArticles.Include(b => b.Tags).SingleOrDefault(b => b.Id == id);

            if (blgs == null)
                return HttpNotFound();

            var viewModel = new BlogArticleViewModel(blgs)
            {
                BlogArticles = blgs,
                Tags = _context.Tags.ToList()
            };

            return View("BlogArticleForm", viewModel);
        }

        //GET : BlogArticles

        [HttpGet]
        [Authorize(Roles = RoleName.CanManageEverything)]
        public ActionResult Save()
        {
            BlogArticle blg = new BlogArticle();
            return RedirectToAction("Blog", "BlogArticle", blg);
        }

        //POST : BlogArticles

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [Authorize(Roles = RoleName.CanManageEverything)]
        public ActionResult Save(BlogArticle blg, string articleImage, HttpPostedFileBase imageFile)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BlogArticleViewModel(blg)
                {
                    BlogArticles = blg,
                    Tags = _context.Tags.ToList()
                };

                return View("BlogArticleForm", viewModel);
            }

            if (imageFile == null)
            {
                ModelState.AddModelError("MovieImage", "Παρακαλώ ανεβάστε μία εικόνα άρθρου.");
            }

            // Upload Image //

            if (imageFile != null)
            {
                articleImage = Path.GetFileNameWithoutExtension(imageFile.FileName);
                string extension = Path.GetExtension(imageFile.FileName);
                articleImage = articleImage + DateTime.Now.ToString("yymmssfff") + extension;
                blg.ArticleImage = "~/images/BlogImages/" + articleImage;
                articleImage = Path.Combine(Server.MapPath("~/images/BlogImages/") + articleImage);
                imageFile.SaveAs(articleImage);
            }

            if (blg.Id == 0)
            {
                blg.DateAdded = DateTime.Now;
                _context.BlogArticles.Add(blg);
            }

            else
            {
                var blginDb = _context.BlogArticles.Single(b => b.Id == blg.Id);

                blginDb.ArticleTitle = blg.ArticleTitle;
                blginDb.ArticleText = blg.ArticleText;
                blginDb.ArticleSource = blg.ArticleSource;
                blginDb.ArticleIntro = blg.ArticleIntro;
                blginDb.ArticleImage = blg.ArticleImage;
                blginDb.TagsId = blg.TagsId;
            }

            _context.SaveChanges();
            return RedirectToAction("Blog", "BlogArticle");
        }

        //GET : BlogArticles/Delete

        [HttpGet]
        [Authorize(Roles = RoleName.CanManageEverything)]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            BlogArticle blg = _context.BlogArticles.Find(id);

            if (blg == null)
            {
                return HttpNotFound();
            }
            return View(blg);
        }

        // POST: BlogArticles/Delete/

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = RoleName.CanManageEverything)]
        public ActionResult DeleteConfirmed(BlogArticle blg, int id, string imageFile)
        {
            imageFile = "";
            string path = Server.MapPath(@"~/images/BlogImages/" + imageFile);

            System.IO.File.SetAttributes(path, FileAttributes.Normal);
            System.IO.File.Delete(path);

            blg = _context.BlogArticles.Find(id);

            _context.BlogArticles.Remove(blg);
            _context.SaveChanges();

            return RedirectToAction("Blog", "BlogArticle");
        }
    }
}

课程

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace SkylineNew.Models
{
    public class BlogArticle
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Παρακαλώ εισάγετε το κείμενο του άρθρου")]
        [Display(Name = "Κείμενο")]
        public string ArticleText { get; set; }

        [Required(ErrorMessage = "Παρακαλώ εισάγετε τον τίτλο του άρθρου")]
        [Display(Name = "Τίτλος")]
        public string ArticleTitle { get; set; }

        [Required(ErrorMessage = "Παρακαλώ εισάγετε την πηγή του άρθρου")]
        [Display(Name = "Πηγή")]
        public string ArticleSource { get; set; }

        [Display(Name = "Εικόνα Άρθρου")]
        public string ArticleImage { get; set; }

        [Required(ErrorMessage = "Παρακαλώ εισάγετε την περίληψη του άρθρου")]
        [Display(Name = "Περίληψη κειμένου")]
        public string ArticleIntro { get; set; }

        public DateTime DateAdded { get; set; }

        public Tags Tags { get; set; }

        [Required(ErrorMessage = "Παρακαλώ εισάγετε ετικέτες.")]
        [Display(Name = "Ετικέτες")]
        public byte TagsId { get; set; }
    }
}

Web.Config

<?xml version="1.0" encoding="utf-8"?>
<!--
 For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=301880
-->

<configuration>
    <configSections>
        <!-- For more information on Entity Framework configuration, visit  http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework"  type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,  EntityFramework, Version=6.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="SkylineNewDB" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SkylineNewDB.mdf;Initial Catalog=SkylineNewDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />
        <httpModules>
            <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
        </httpModules>
    </system.web>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
            <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
            <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <system.webServer>
        <modules>
            <remove name="TelemetryCorrelationHttpModule" />
            <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
            <remove name="ApplicationInsightsWebTracking" />
            <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
        </modules>
        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
    </system.codedom>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="mssqllocaldb" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
</configuration>

2 个答案:

答案 0 :(得分:0)

对于您的ActionResult DeleteConfirmed,我将添加一个Try语句以捕获任何失败,并且还将在尝试删除该文件之前检查该文件是否存在:

try
    {
        if (System.IO.File.Exists(path))
        {
            System.IO.File.SetAttributes(path, FileAttributes.Normal);
            System.IO.File.Delete(path);
        }
    }
    catch (Exception e)
        {
            // Handle error
        }

您正在获得的“访问被拒绝”听起来像是在运行您的网站的用户帐户,但没有足够的权限删除指定文件夹中的文件。您需要检查托管服务器上的安全性。

答案 1 :(得分:0)

我刚刚找到了解决方案!!!!

所以在控制器的DeleteConfirmed Action中,我做了一些更改。

 [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = RoleName.CanManageEverything)]
    public ActionResult DeleteConfirmed(BlogArticle blg, int id)
    {                      
        blg = _context.BlogArticles.Find(id);

        _context.BlogArticles.Remove(blg);

        System.IO.File.Delete(Request.MapPath(blg.ArticleImage));

        _context.SaveChanges();

        return RedirectToAction("Blog", "BlogArticle");
    }