简体   繁体   English

如何知道控制器中的方法何时完成

[英]How to know when a method finish in controller

I have a problem with this method, i want to return a PDF file and when the method it's over i want to delete de file from the directory. 我对此方法有疑问,我想返回一个PDF文件,方法结束后,我想从目录中删除文件。

public ActionResult DescargaPdfCompara(string id)
    {
        var rutaPdf = string.Empty;
        var type = "application/pdf";

        try
        {

            DateTime ahora = DateTime.Now;
            var numeroAleatorio = new Random();
            int numeroRandomico = numeroAleatorio.Next(100000000, 1000000000);
            string Ruta = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Reportes\" + Convert.ToString(ahora.Year + ahora.Month + ahora.Day + ahora.Hour + ahora.Minute + ahora.Second + numeroRandomico) + ".pdf");

            var result = SimuModel.ObtenerSabanaReporteComparativo(id);
            var resumen = SimuModel.ObtenerPreExcel(result);
            SimuModel.GenerarPdfCompa(result, resumen, Ruta);

            rutaPdf = Ruta;

            return File(rutaPdf, type);

        }
        catch (Exception e)
        {

            throw e;
        }
        finally
        {
            System.IO.File.Delete(rutaPdf);
        }
    }

In the finally i delete the file but i got an error because the method can't find the file, for some reason the method delete the file before return it. 最后,我删除了文件,但是由于方法找不到文件而报错,由于某种原因,该方法在返回文件之前先删除了文件。

PD: Sorry for my english, i'm from Chile. PD:对不起,我的英语,我来自智利。

Thanks fro your answers! 感谢您的回答!

You can use System.IO.File.ReadAllBytes to read all the file contents to memory, then delete the file and return the contents with another overload of Controller.File method: 您可以使用System.IO.File.ReadAllBytes将所有文件内容读取到内存中,然后删除该文件并返回带有另一个Controller.File方法重载的内容:

    public ActionResult GetFile()
    {
        var fileName = Path.GetTempFileName();
        System.IO.File.WriteAllText(fileName, "Hola, Chile!");
        var bytes = System.IO.File.ReadAllBytes(fileName);
        System.IO.File.Delete(fileName);
        return File(bytes, "text/plain", "file.txt");
    }

Change return type ContentResult 更改返回类型ContentResult

remove finally section. 最终删除部分。

public ContentResult DescargaPdfCompara(string id)
{
    var rutaPdf = string.Empty;
    var type = "application/pdf";

    try
    {

        DateTime ahora = DateTime.Now;
        var numeroAleatorio = new Random();
        int numeroRandomico = numeroAleatorio.Next(100000000, 1000000000);
        string Ruta = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Reportes\" + Convert.ToString(ahora.Year + ahora.Month + ahora.Day + ahora.Hour + ahora.Minute + ahora.Second + numeroRandomico) + ".pdf");

        var result = SimuModel.ObtenerSabanaReporteComparativo(id);
        var resumen = SimuModel.ObtenerPreExcel(result);
        SimuModel.GenerarPdfCompa(result, resumen, Ruta);

        rutaPdf = Ruta;

        return Content(rutaPdf, type);

    }
    catch (Exception e)
    {

        throw e;
    }

}

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

相关问题 api控制器如何知道不带注释的调用方法 - How does the api controller know which method to call without the annotation 如果我在调用中使用相同的URL,我的控制器如何知道何时在另一方法上执行一个方法(更新/插入)? - How does my controller know when to execute one method over another (update/insert) if I have the same URL on the call? 如何知道文本框读完所有文件 - How to know the textbox finish to read all the file 如何知道何时执行类中的方法 - How to know when a method in a class is being executed 从Controller返回而无需等待异步方法完成 - Return from Controller without waiting for async method to finish 在.net核心中调用外部服务时,如何等待异步方法完成? - How to wait for async method to finish when calling a external service in .net core? 定时方法调用-如何在时间到期时继续前进而不等待操作完成? - Timed Method Calls - How do I move on when time expires without waiting for operation to finish? 如何知道何时在可观察集合中调用基本方法或重写方法 - How to know when to call base method or override methods in observable collection 如何知道何时在我的应用程序代码中调用任何方法? - How to know when any method is called in my application code? 如何知道使用异步时Task或Void方法是否仍在运行 - How to know if Task or Void method is still running when using Async
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM