0

私のasp mvc 3アプリケーションには、ユーザーが特定のファイルをダウンロードできるようにするアクションがあります。

コードは次のとおりです。

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 未満のファイルのみをダウンロードできるようにしたいと考えています。しかし、このタイプのファイルのサイズを計算する方法が見つかりません。

何か案は ?

4

1 に答える 1

3

私はこれがうまくいくと思います:

FileInfo file = new FileInfo(uploadsDocumentPath + fileName);
if(file.Length > 157286400)
{
      // Return error here.
}
于 2012-07-30T13:15:59.457 に答える