2016-05-13 68 views
0

我可以在通過電子郵件發送消息時上傳/附加文件。這些文件存儲在App_Data/uploads文件夾中,所以當我試圖發送多個文件時,需要很長時間才能發送它。我認爲這是因爲該文件夾已經有很多文件,所以我想刪除文件夾中的文件,當它已經通過電子郵件發送。請幫幫我。我只是新的這種東西。謝謝!這裏的控制器:如何刪除「/ App_Data/uploads」文件夾中上傳的文件ASP.NET MVC

[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); 

    } 

回答

1

intially檢查文件是否存在,然後嘗試下面的代碼

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

謝謝你的幫忙。 –

1

到電子郵件,你必須先檢查發送之前...

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

謝謝你的幫忙。 –

+0

你最歡迎... @ KenHemmo –

1

試試這個:

System.IO.DirectoryInfo di = new DirectoryInfo(path); 

foreach (FileInfo file in di.GetFiles()) 
{ 
    file.Delete(); 
} 
+0

謝謝你的幫忙。 –

0

我強烈推薦修補不使用App_Data文件夾來存儲任何文件,按照慣例僅爲存儲數據庫文件而創建。