简体   繁体   English

发送电子邮件ASP.NET MVC 5后删除Rotativa pdf

[英]Delete Rotativa pdf after sending email ASP.NET MVC 5

I am able to generate pdf using Rotativa and using the code below. 我可以使用Rotativa并使用以下代码生成pdf。 So I save the pdf and email as an attachment. 因此,我将pdf和电子邮件另存为附件。 Now my problem is how to delete this file. 现在我的问题是如何删除此文件。 The file generation and emailing could take a while so when the delete call is made it generates an error: The process cannot access the file ....blah/blah.pdf because it is being used by another process. 文件生成和发送电子邮件可能需要一段时间,因此在进行删除调用时会产生错误:该进程无法访问文件.... blah / blah.pdf,因为该文件正在被另一个进程使用。

I need help to be able to clean up the files generated. 我需要帮助才能清理生成的文件。 These files span a years worth of records and take up quite a bit of space and time to generate. 这些文件记录了一年的时间,并且占用了相当多的空间和时间来生成。

How do I make the delete call take place when the file has been completely emailed and released? 完全通过电子邮件发送并释放文件后,如何进行删除调用?

public ActionResult Receipt(int year, int Id, ReceiptViewModel model)
        {
            var root = Server.MapPath("~/App_Data/");
            var pdfname = String.Format("{0}-{1}-{2}.pdf", Id, year, Guid.NewGuid().ToString());
            var path = Path.Combine(root, pdfname);
            path = Path.GetFullPath(path);
            IUserMailer mailer = new UserMailer();


            var a = new ViewAsPdf("Receipt", new { Id, year })
            {
                FileName = pdfname,
                PageSize = Rotativa.Options.Size.A5,
                PageOrientation = Rotativa.Options.Orientation.Portrait,
                SaveOnServerPath = path
            };

            var byteArray = a.BuildFile(ControllerContext);
            var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
            fileStream.Write(byteArray, 0, byteArray.Length);
            fileStream.Close();
       // email the pdf as attachment  
      mailer.BulkReceipt(path, pdfname, Id, year).SendAsync();
      // Delete file
System.IO.File.Delete(path);
            return View("ReceiptSuccess", a);


        }

You could..., make an in mem copy of the file and then tell the mailer to use that version(in mem).... which means you can delete the local physical file. 您可以...,在文件的内存副本中创建一个副本,然后告诉邮件程序使用该版本(在内存中)...。这意味着您可以删除本地物理文件。 the in mem version will clean itself up once no-longer used. 一旦不再使用in in mem版本,它将自行清理。

Usually a method with Async in its name will call a callback method when it is complete or return a Task. 通常,名称为Async的方法将在完成时调用回调方法或返回Task。

Often the callback will be passed into the method, something like below: 通常,回调将传递到方法中,如下所示:

mailer.BulkReceipt(path, pdfname, Id, year).SendAsync(() => System.IO.File.Delete(path));

But sometimes there is a separate event that you need to wire up on your object with the callback for the when it is finished. 但是有时有时需要单独的事件,您需要在对象完成时使用回调进行连接。 In this case it could be called SendComplete or something similar. 在这种情况下,它可以称为SendComplete或类似名称。

If the SendAsync call returns a Task, then you probably want to call await and make your Receipt method async: 如果SendAsync调用返回一个Task,那么您可能想调用await并使您的Receipt方法异步:

public async Task<ActionResult> Receipt(int year, int Id, ReceiptViewModel model)
{
    ...
    await mailer.BulkReceipt(path, pdfname, Id, year).SendAsync();
    // Delete file
    System.IO.File.Delete(path);

    return View("ReceiptSuccess", a);
}

Or something similar to that... 或类似的东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM