HttpPostedFileBase ContentLength is different than actual file size

358 views Asked by At

I am using HttpPostedFileBase to upload file on server.

I am passing the file content in bytes:

HttpPostedFileBase MyFile { get; set; }

byte[] target;
using (var ms = new MemoryStream())
{
    MyFile.InputStream.CopyTo(ms);
    target = ms.toArray();
}

var upload = UploadDocument(fileName, target);

I am validating the uploaded file size <= 4MB (4194304 Bytes).

But when I try to upload the file of size 7MB, the HttpPostedFileBase ContentLength property is returning as 2165042 Bytes, which is less than 7MB.

This fails my <= 4MB file size validation.

Why is the ContentLength not same as the actual file size? How do I check the actual file size of the HttpPostedFileBase file?

0

There are 0 answers