【问题标题】:How to get the actual attachment size or total size of attached files如何获取附件的实际大小或附件的总大小
【发布时间】:2014-05-13 18:05:48
【问题描述】:

大家好,我有一个 DataTable,它保存文件名,路径如下

String[] sizeArry = new String[] { "Byes", "KB", "MB" };

    String GetSize(ulong sizebytes, int index)
    {

        if (sizebytes < 1000) return sizebytes + sizeArry[index];

        else return GetSize(sizebytes / 1024, ++index);
    }

protected DataTable GetAttachment()
    {
        DataTable attachment = new DataTable();
        attachment.Columns.Add("ID", typeof(int));
        attachment.Columns.Add("Attachment", typeof(string));
        attachment.Columns.Add("Size", typeof(string));

        attachment.Rows.Add(1, " files/abc.txt");
        attachment.Rows.Add(2, "files/test.pdf");

        foreach (DataRow row in attachment.Rows)
        {
            string sFilename = row["Attachment"].ToString();
            string path = Server.MapPath(sFilename);
            FileInfo fi = new FileInfo(path);
            row["Attachment"] = fi.Name;
            row["Size"] = GetSize((ulong)fi.Length, 0);
        }
        Total(attachment);
        return attachment;
    }

这给我的结果如下abc.txt 3KB test.pdf 11MB 现在我需要总结这些尺寸并需要在label 中显示尺寸所以有人可以帮助我。我尝试如下,但没有按要求工作

protected void Total(DataTable dt)
    {
        int size = 0;
        int cnt = 0;
        foreach (DataRow row in dt.Rows)
        {
            cnt++;
            string s = row["Size"].ToString().Remove(row["Size"].ToString().Length - 2);
            size = size + Convert.ToInt16(s);
        }
       label.Text = cnt.ToString() + " attachments " + size.ToString();
    }

【问题讨论】:

  • 这是什么意思,它没有按要求工作?总和错了?还是?
  • 是的 sum 是错误的,它显示 14mb 这是错误的

标签: c# asp.net


【解决方案1】:

首先你要检查是否是BytesKiloBytesMegaBytes等等。

如果你知道它是什么,你可以调用你的辅助方法,像这样从 Bytes->KiloBytes->MegaBytes 转换:

public static class Converter
{
    public static double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }

    public static double ConvertKilobytesToMegabytes(long kilobytes)
    {
        return kilobytes / 1024f;
    }  
}

编辑:

您不必计算行数,有一个属性:

label = dt.Rows.Count +" attachments " + size.ToString();

编辑2:

这样的事情可能会奏效。

protected void Total(DataTable dt)
{
    string label;
    double size = 0;

    foreach (DataRow row in dt.Rows)
    {
        var result = Regex.Match(row["Size"].ToString(),@"([0-9]+)(.*)");
        var fileSize = Convert.ToInt32(result.Groups[0].Value);
        var type = result.Groups[1].Value;

        switch (type)
        {
            case "B":
                size += ConvertBytesToMegabytes(fileSize);
                break;
            case "KB":
                size += ConvertKilobytesToMegabytes(fileSize);
                break;
            case "MB":
                size += fileSize;
                break;
            //ETC...
            default:
                break;
        }
    }
    label = dt.Rows.Count +" attachments " + size.ToString();       
}

【讨论】:

  • 但根据数据表,我知道我需要调用哪种方法
  • @Learner 我已经编辑了答案...不确定它是否适用于 100%,但您可以从中获得灵感
猜你喜欢
  • 2023-04-11
  • 2013-02-06
  • 2019-12-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2011-06-11
  • 1970-01-01
相关资源
最近更新 更多