【问题标题】:How to delete the uploaded files in the "/App_Data/uploads" folder ASP.NET MVC如何删除“/App_Data/uploads”文件夹 ASP.NET MVC 中上传的文件
【发布时间】:2016-05-14 03:22:23
【问题描述】:

当我通过电子邮件发送消息时,我可以上传/附加文件。这些文件存储在 App_Data/uploads 文件夹中,因此当我尝试发送多个文件时,需要很长时间才能发送。我认为这是因为文件夹已经有很多文件,所以我想在文件夹中已经发送电子邮件时删除文件夹中的文件。请帮我。我对这种东西很陌生。谢谢!这是控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
{
    if (ModelState.IsValid)
    {

        List<string> paths = new List<string>();

        foreach (var file in files)
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                file.SaveAs(path);

                paths.Add(path);
            }

        }

            var message = new MailMessage();
            foreach (var path in paths)
            {
                var fileInfo = new FileInfo(path);
                var memoryStream = new MemoryStream();
                using (var stream = fileInfo.OpenRead())
                {
                    stream.CopyTo(memoryStream);
                }
                memoryStream.Position = 0;
                string fileName = fileInfo.Name;
                message.Attachments.Add(new Attachment(memoryStream, fileName));
            }

            //Rest of business logic here
            string EncodedResponse = Request.Form["g-Recaptcha-Response"];
            bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
            if (IsCaptchaValid)
            {

                var body = "<p>Email From: {0} ({1})</p><p>Subject: {2} </p><p>Message:</p><p>{3}</p>";
                message.To.Add(new MailAddress("***@gmail.com"));  // replace with valid value 
                message.From = new MailAddress("***@ymailcom");  // replace with valid value
                message.Subject = "Your email subject";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
                message.IsBodyHtml = true;
                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "***@gmail.com",  // replace with valid value
                        Password = "***"  // replace with valid value
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    //return RedirectToAction("Sent");
                    ViewBag.Message = "Your message has been sent!";

                    //TempData["message"] = "Message sent";
                    ModelState.Clear();
                    return View("Index");
                }

            }
            else
            {
                TempData["recaptcha"] = "Please verify that you are not a robot!";
            }
        }
        return View(model);

    }

【问题讨论】:

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


    【解决方案1】:

    首先检查文件是否存在,然后尝试下面的代码

    File.Delete("~/App_Data/uploads/XXXXX.xls");
    

    【讨论】:

    • 谢谢先生的帮助。
    【解决方案2】:

    在通过电子邮件发送之前,您必须先检查...

    if (System.IO.File.Exists(fullPath of your file))
                {
                    System.IO.File.Delete(fullPath of your file);
                }
    

    【讨论】:

    • 谢谢先生的帮助。
    【解决方案3】:

    试试这个:

    System.IO.DirectoryInfo di = new DirectoryInfo(path);
    
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete(); 
    }
    

    【讨论】:

    • 谢谢先生的帮助。
    【解决方案4】:

    我强烈建议不要使用 App_Data 文件夹来存储任何文件,按照惯例,它只用于存储数据库文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2016-09-07
      • 2017-04-02
      相关资源
      最近更新 更多