-3

This is my code:

if (btnfileupload.HasFile)
{
    try
    {
        Int64 _size = 0;
        string strsize = null;
        int y = 0;

        _size = btnfileupload.PostedFile.ContentLength;

        strsize = _size.ToString();
        if (strsize.Contains("."))
        {
            y = strsize.IndexOf(".");
            strsize = strsize.Substring(0, y - 1);
        }

        Int64 _accountno = (Int64)Session["aco"];
        home h = new home();
        h._Account_number = _accountno;
        h._FileName = Path.GetFileName(btnfileupload.FileName);
        h._file_size = strsize;
        h._uploadDate = DateTime.Now;
        bool b = h.FileuploadSave(firstfilename);

        if (b)
            Response.Write("<script> aleart('File Uploaded') </script>");

        Fillgrid(null, null);
    }
    catch (Exception)
    {

    }
}

btnfileupload.PostedFile.ContentLength return the size in bytes. The problem is if file size is 213.562 bytes then this property returns 213562 there is no dot(.) in return size. Please give me code that returns exact size.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • 1
    A file cannot have a fractional number of bytes. I assume the dot is a thousands separator in your culture? – Douglas Sep 25 '13 at 08:23
  • [http://stackoverflow.com/questions/565704/how-to-correctly-convert-filesize-in-bytes-into-mega-or-gigabytes](http://stackoverflow.com/questions/565704/how-to-correctly-convert-filesize-in-bytes-into-mega-or-gigabytes) – Pascalz Sep 25 '13 at 08:24
  • FYI there's a typo in your `Response.Write`, it should be `alert` instead of `aleart`. – user247702 Sep 25 '13 at 08:24
  • possible duplicate of [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – user247702 Sep 25 '13 at 08:37

2 Answers2

1
strsize = _size.ToString();
if (strsize.Contains("."))
{
    y = strsize.IndexOf(".");
    strsize = strsize.Substring(0, y - 1);
}

That part of the code looks badly broken, even if the ToString result were to include '.' as a thousands separator, since it does not allow you to determine whether you're ending with bytes, kilobytes, megabytes, or gigabytes. If you always want to return kilobytes, you just need to divide by 1024:

sizeKB = _size / 1024;    // Rounds down to nearest kilobyte.

On the other hand, if you want to adapt your unit depending on the order of magnitude of the file, you'll need to include some additional logic.

Douglas
  • 53,759
  • 13
  • 140
  • 188
-1

I don't think file size in bytes can have a decimal (correct me if I am wrong) unless they are converted into KB,MB etc

EDIT: As Stijn said (.) is a separator in bytes

Rafay
  • 603
  • 2
  • 9
  • 24
  • It's not a decimal, it's a thousands separator. – user247702 Sep 25 '13 at 08:31
  • @Stijn isnt this what I said in answer ?? or should I be writing it more clearly ? – Rafay Sep 25 '13 at 08:41
  • The problem isn't about a decimal separator. OP just wants a thousands separator, which can be achieved by the duplicate I linked. For example, if the file size is a hundred thousand bytes, OP wants *100.000 bytes* Your answer is technically correct, but doesn't answer the question. – user247702 Sep 25 '13 at 08:43