0

I am working with a Visual Studio model, and I have a page for "downloads" that displays file name, file size, and a link. However, I am trying to convert the file size to a readable format. I have found several methods for doing this, but I think where I am getting stuck is how to incorporate that into the code that I am already using. For instance, I already have a FileSize variable, so I want to incorporate that into the code for the conversion. Here is my existing code for the model itself:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Hosting;

namespace SupportToolkit.Models
{
public class DataClassesModel
{
    public List<FileNames> GetFiles()
    {

        List<FileNames> lstFiles = new List<FileNames>();
        DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/Files"));

        int i = 0;
        foreach (var item in dirInfo.GetFiles())
        {

            lstFiles.Add(new FileNames()
            {
                FileId = i + 1,
                FileName = item.Name,
                FileSize = item.Length,
                FilePath = dirInfo.FullName + @"\" + item.Name,
                FileExtension = item.Extension
            });
            i = i + 1;
        }

        return lstFiles;
    }
}



public class FileNames
{
    public int FileId { get; set; }
    public string FileName { get; set; }
    public long FileSize { get; set; }
    public string FilePath { get; set; }
    public string FileExtension { get; set; }
    }
}

I want to add some code in here to do the conversion to KB, MB, GB, etc. - and I have an idea, but don't know how to incorporate it into what I already have. Here is an example of what I am going for with conversion:

public static string FileSizeFormat(this long FileSize)
    {
        double size = FileSize;
        int index = 0;
        for (; size > 1024; index++)
            size /= 1024;
        return size.ToString("0.00 " + new[] { "B", "KB", "MB", "GB", "TB" }[index]);
    }

Any help would be greatly appreciated! I am very new to this and am just stuck on how to blend these two things together. :)

Thank you.

Let me also add the code used in Index.html, maybe that will help complete the circle. I am trying to figure out how to get the formatted file size to show on this page. Thank you.

@using SupportToolkit.Models
@model IEnumerable<SupportToolkit.Models.FileNames>

@{
ViewBag.Title = "Index";
}

<h2>Downloads</h2>

<hr />

<table>
<tr>
    <th>
        File Name
    </th>

    <th>
        File Size
    </th>
</tr>

@foreach (var item in Model)
{
    <tr >
        <td>
            @Html.DisplayFor(modelItem => item.FileName)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.FileSize)
        </td>

        <td>
            <p style="text-indent: 5em;">
                @Html.ActionLink("Download", "Download", new { id = item.FileId })
            </p>
        </td>
    </tr>
}

In this instance, I am using @Html.DisplayFor(modelItem => item.FileSize) to display the file size. But that does not include the formatting. How would I make this the formatted file size (human-readable)?

LilithGoddess
  • 125
  • 1
  • 5
  • 16
  • Possible duplicate of [How do I get a human-readable file size in bytes abbreviation using .NET?](http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net) – Geoff James Jun 26 '16 at 17:35
  • I understand how to get the human-readable file size. What I do not understand is how to incorporate that into the code I am already using. I was very clear about explaining that. In fact, the link you posted I believe is where I got the code to use for how to convert the file size. That is not the issue. The issue is that I am looking for some guidance on how to combine this method with the code I have already written. I posted both my existing code and the method I want to use for conversion. The question is how do I use them together? – LilithGoddess Jun 26 '16 at 18:28
  • Specifically - how do I take the "FileSize = item.length" I am already using and convert that to human-readable? How would my current code incorporate the conversion? – LilithGoddess Jun 26 '16 at 18:31
  • @LilithGoddess: it's not exactly clear what you're asking. Obviously, invoking `FileSizeFormat` with the `FileSize` (i.e. `item.Length`) parameter will return the `string` representation you are asking for. But it's impossible to tell what you actually want to do with this string. Dump it to a console? Save it to a text file? Or do you want to add a new `string` property inside `FileNames` and so that `GetFiles` assigns the string value to this property? Usually, you do the formatting at the end (before printing/rendering). Btw. better name for the class should be `FileName`, singular. – vgru Jun 26 '16 at 18:46
  • Also, you don't need to concatenate `dirInfo.FullName + @"\" + item.Name`, just use `item.FullName` instead, it already contains the full path. – vgru Jun 26 '16 at 18:49
  • I can further explain, thanks for the question! with that string, I would be using it in an "index.html" and calling it to be displayed in a table. i.e. "@Html.DisplayFor(modelItem => item.FileSizeFormat)". – LilithGoddess Jun 26 '16 at 19:38

2 Answers2

0

Looks like you defined FileSizeFormat as an extension method of long.

In that case, you can call, e.g., lstFiles[0].FileSize.FileSizeFormat() and it will return the readable string.

Don't forget to add using XXX where XXX is the namespace where you defined FileSizeFormat method.

Consider renaming FileSizeFormat to ToReadableFileSize(). It will make your code more 'readable' - lstFiles[0].FileSize.ToReadableFileSize().

daramasala
  • 3,040
  • 2
  • 26
  • 33
  • Thank you - so, my question is. Currently, in index.html, the way I am displaying the file size in a table is by using "@Html.DisplayFor(modelItem => item.FileSize)". This works fine, except does not contain the formatting. So I thought I could use "@Html.DisplayFor(modelItem => item.FileSize.FileSizeFormat)" to include the formatting, but that does not compile. Am I doing this incorrectly? – LilithGoddess Jun 26 '16 at 19:44
0

Figured it out, after much time. Figured I would post just so others could learn from it that may have the same learning curve. ;)

I changed just one line of code. In the model itself:

FileSize = item.Length,

Now reads:

FileSize = FileSizeFormat(item.Length),

I also removed the static, from the public string, as it was not needed.

Nothing else had to change. That's it. Thanks!

LilithGoddess
  • 125
  • 1
  • 5
  • 16