2012-10-12 108 views
2

我有一個方法在我的控制器上傳方法,它將上傳的圖像文件轉換成三種不同的大小。我想dispaly從所花費的時間,當文件被上傳並和轉換停止方法結束任何一個可以告訴我,我怎麼能做到這一點如何在ASP.Net MVC中獲取方法的開始時間和結束時間?

 this is my controller 


      public ActionResult Upload() 
    { 
     //ViewBag.ProcessingTime = DateTime.Now; 
     return View(); 
    } 

     [HttpPost] 
     public ActionResult Uploading(ImageModel model) 
     { 
      if (ModelState.IsValid) 
      { 

       Console.WriteLine("time is"+System.DateTime.Now); 

       string fileName = Guid.NewGuid().ToString() + System.DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss/fff"); 
       string serverPath = Server.MapPath("~"); 
       string imagesPath = serverPath + "Content\\Images\\"; 
       string thumsise = Path.Combine(imagesPath, "Thumb" + fileName); 
       string thumbPath = Path.Combine(imagesPath, "Thu" + fileName); 
       //string thumbPath = imagesPath + "Thumb\\"; 
       string fullPath = Path.Combine(imagesPath, "Full" + fileName); 
       //string fullPath = imagesPath + "Full\\"; 
       // string Bigpath = imagesPath + "big\\"; 
       string Bigpath = Path.Combine(imagesPath, "big" + fileName); 
       string Bigpatha = Path.Combine(imagesPath, "biga" + fileName); 
       string Bigpathb = Path.Combine(imagesPath, "bigb" + fileName); 
       //string Bigpatha = imagesPath + "biga\\"; 
       //string Bigpathb = imagesPath + "bigb\\"; 
       string Bigpathc = Path.Combine(imagesPath, "bigc" + fileName); 
       //string Bigpathc = imagesPath + "bigc\\"; 
       //var firstSize = Path.Combine(uploadFolder, "bigsize-" + Path.GetFileName(uploadedFile)); 
       ImageModel.ResizeAndSave(thumsise, fileName, model.ImageUploaded.InputStream, 80, true); 
       ImageModel.ResizeAndSave(thumbPath, fileName, model.ImageUploaded.InputStream, 100, true); 
       ImageModel.ResizeAndSave(fullPath, fileName, model.ImageUploaded.InputStream, 500, true); 
       ImageModel.ResizeAndSave(Bigpath, fileName, model.ImageUploaded.InputStream, 200, true); 
       ImageModel.ResizeAndSave(Bigpatha, fileName, model.ImageUploaded.InputStream, 250, true); 
       ImageModel.ResizeAndSave(Bigpathb, fileName, model.ImageUploaded.InputStream, 150, true); 
       ImageModel.ResizeAndSave(Bigpathc, fileName, model.ImageUploaded.InputStream, 50, true); 
       Console.WriteLine("Time is " + System.DateTime.Now); 
      } 
      Console.WriteLine("Time is "+System.DateTime.Now); 

      Console.ReadLine(); 
      return View(); 
     } 

回答

0

您必須添加額外的字段在Model

public DateTime StartTime { get; set; } 
public DateTime OperatingTime { get; set; } 
public DateTime EndTime { get; set; } 

然後適當地分配給每個,即StartTime = DateTime.Now;

然後在您的View

@Html.DisplayFor(m=>m.StartTime); 

附加

我會親自包上傳圖像的功能集成到一個新的方法,最好是在你的ImageModel類。然後,你可以這樣做:model.UploadImage(fileName, imagePath);

0

使用秒錶

var stopwatch = new Stopwatch(); 
    stopwatch.Start();  
    //whatever you want to time 
    stopwatch.Stop(); 
    var result1 = string.Format("{0} to run whatever ", 
           stopwatch.Elapsed.ToString()); 
    Console.WriteLine(result1); 

多計時您可以重置秒錶做類似於第一個更時序。

+0

我必須做什麼「{0}」來運行任何「此地點 – SoftwareNerd

+0

{0}將被stopwatch.Elapsed.ToString()替換。如果你喜歡,我會更新使用你的控制檯 – dove

相關問題