【问题标题】:How to know when a method finish in controller如何知道控制器中的方法何时完成
【发布时间】:2015-02-16 15:35:32
【问题描述】:

我对这个方法有问题,我想返回一个 PDF 文件,当方法结束时,我想从目录中删除 de 文件。

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

最后我删除了文件,但由于该方法找不到文件而出现错误,由于某种原因该方法在返回之前删除了文件。

PD:对不起我的英语,我来自智利。

感谢您的回答!

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 controller


    【解决方案1】:

    您可以使用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");
        }
    

    【讨论】:

    • @Maurinostroza podría hacer clic en "aceptar respuesta"
    【解决方案2】:

    更改返回类型 ContentResult

    删除 finally 部分。

    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;
        }
    
    }
    

    【讨论】:

    • 删除 finally 部分后文件恢复正常,问题是我必须删除它,谢谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多