2012-07-30 72 views
0

在我的asp mvc 3应用程序中,我有一个允许用户下载给定文件的动作。如何计算C#中的文件大小

下面是代码:

public FilePathResult DownloadFile(string fileName) 
    { 
     try 
     { 
      string uploadsDocumentPath = System.Configuration.ConfigurationManager.AppSettings["uploadsDocumentPath"].ToString(); 
      string ext = Path.GetExtension(fileName).ToLower(); 
      Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); // henter info fra windows registry 
      if (regKey != null && regKey.GetValue("Content Type") != null) 
      { 
       mimeType = regKey.GetValue("Content Type").ToString(); 
      } 

      return File(uploadsDocumentPath + fileName, mimeType, fileName); 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

我希望能够允许尺寸小于要下载150MB仅文件。但我找不到如何计算这种类型的文件的大小。

任何想法?

回答

3

我想这应该工作:

FileInfo file = new FileInfo(uploadsDocumentPath + fileName); 
if(file.Length > 157286400) 
{ 
     // Return error here. 
} 
+0

工作就像一个魅力,谢谢。 – kbaccouche 2012-07-30 13:29:26

+0

没问题!很高兴我可以帮助:) – 2012-07-30 13:29:56

+2

150000000不是150MB。 157286400是150MB = 1024x1024 x150 – 2012-07-30 13:48:02