Get a descriptive size string for your uploads

In an ASP.NET page, when you've used a System.Web.UI.HtmlControls.HtmlInputFile control in your page - sometimes you want to get the size of the file uploaded. The HttpPostedFile class has a ContentLength property - but the value attainable by using the .ToString() method leaves something to be desired. So, here is a simple little method I wrote to get you a more descriptive string from the ContentLength value - I thought I'd share it here for anyone that wants to do something similar.

C#:

public static string GetFileSizeStringFromContentLength(int ContentLength)
{
      if(ContentLength > 1048575)
      {
      return String.Format("{0:0.00 mb}", (Convert.ToDecimal((Convert.ToDouble(ContentLength) / 1024) / 1024))) ;
      }
      else
      {
      return String.Format("{0:0.00 kb}", (Convert.ToDecimal(Convert.ToDouble(ContentLength) / 1024))) ;
      }
}

VB.NET:

Public Function GetFileSizeStringFromContentLength(ByVal ContentLength As Integer) As String
   
If (ContentLength > 1048575) Then
      
GetFileSizeStringFromContentLength = String.Format("{0:0.00 mb}", (Convert.ToDecimal((Convert.ToDouble(ContentLength) / 1024) / 1024)))
   
Else
      
GetFileSizeStringFromContentLength = String.Format("{0:0.00 kb}", (Convert.ToDecimal(Convert.ToDouble(ContentLength) / 1024)))
   
End If
End Function

You can adjust the string format to attain more precise size measurements - all I cared to have was something like "230.45 kb" or "2.12 mb" - but you can do whatever you want, of course.

31 Comments

  • And how does that mean? I do not understand anything.

  • Hello! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Thanks!

  • I enjoy you because of all your effort on this website. My niece loves going through investigations and it is easy to understand why. We all learn all of the compelling form you deliver advantageous items via the blog and in addition encourage participation from others about this matter plus our own simple princess has been discovering a great deal. Take pleasure in the rest of the year. You're the one conducting a superb job.

  • This is the suitable weblog for anybody who desires to find out about this topic. You realize so much its virtually arduous to argue with you (not that I truly would want…HaHa). You undoubtedly put a new spin on a subject thats been written about for years. Great stuff, simply great!

  • Please let me know if you're looking for a writer for your blog. You have some really good posts and I think I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to write some material for your blog in exchange for a link back to mine. Please blast me an email if interested. Kudos!

  • I do believe all of the concepts you've presented on your post. They're really convincing and can definitely work. Nonetheless, the posts are too short for beginners. May you please extend them a bit from next time? Thanks for the post.

  • Aw, this was a very nice post. In concept I want to put in writing like this moreover – taking time and precise effort to make an excellent article… but what can I say… I procrastinate alot and certainly not seem to get something done.

  • Thanks for the post. I have usually noticed that most people are needing to lose weight simply because wish to show up slim as well as attractive. Nevertheless, they do not constantly realize that there are more benefits for losing weight additionally. Doctors say that obese people are afflicted with a variety of diseases that can be perfectely attributed to their particular excess weight. The good thing is that people who definitely are overweight as well as suffering from various diseases can reduce the severity of their particular illnesses through losing weight. It's possible to see a gradual but identifiable improvement in health if even a minor amount of weight-loss is realized.

  • You could definitely see your skills in the work you write. The world hopes for more passionate writers like you who aren't afraid to say how they believe. Always go after your heart.

  • I will right away grab your rss as I can not find your email subscription link or newsletter service. Do you've any? Kindly let me know in order that I could subscribe. Thanks.

  • Definitely believe that which you stated. Your favorite justification appeared to be on the web the simplest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they plainly do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks

  • Hi, i think that i saw you visited my blog so i came to “return the favor”.I am attempting to find things to improve my site!I suppose its ok to use some of your ideas!!

  • Hi there! Would you mind if I share your blog with my zynga group? There's a lot of people that I think would really appreciate your content. Please let me know. Cheers

  • I'm very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that's at the other blogs. Appreciate your sharing this greatest doc.

  • We're a group of volunteers and starting a new scheme in our community. Your site offered us with valuable info to work on. You have done an impressive job and our whole community will be grateful to you.

  • This web page is known as a stroll-by way of for the entire information you wished about this and didn’t know who to ask. Glimpse right here, and you’ll undoubtedly uncover it.

  • Heya this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

  • Hey just wanted to give you a quick heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different browsers and both show the same outcome.

  • Thanks for this glorious article. One other thing is that the majority of digital cameras come equipped with a zoom lens that permits more or less of the scene to generally be included by means of 'zooming' in and out. These kinds of changes in target length are usually reflected from the viewfinder and on big display screen right at the back of the specific camera.

  • F*ckin’ amazing things here. I am very glad to see your article. Thanks a lot and i'm looking forward to contact you. Will you please drop me a e-mail?

  • Hi there! Someone in my Myspace group shared this site with us so I came to take a
    look. I'm definitely loving the information. I'm book-marking and will be tweeting this to my followers!
    Great blog and fantastic design.

  • These are really wonderful ideas in about blogging. You have touched some nice points here.
    Any way keep up wrinting.

  • Любой ремонт холодильников в любом округе сделает быстрый мастер в течении восьми часов после заявки. На нашем ресурсе вы можете увидеть прайс с ценами.

  • Простой ремонт холодильников в Подмосковье произведет быстрый мастер в течении пятнадцати часов после заявки. На нашем сайте вы можете скачать прайс с расценками.

  • This is really awsome and i love that.. This is very unique thing you put on that post.. Thanks for sharing...

  • Hello, I enjoy reading all of your article. I like to write
    a little comment to support you.

  • I constantly emailed this webpage post page to all my contacts, because if like to read it next my contacts will
    too.

  • Amazing blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would
    really make my blog stand out. Please let me know where you got
    your design. With thanks

  • Great blog here! Also your website loads up fast!
    What web host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as quickly as yours lol

  • Aw, this was an exceptionally nice post. Finding the time
    and actual effort to produce a really good article… but
    what can I say… I put things off a whole lot and don't manage to get nearly anything done.

  • That is a very good tip especially to those new to the blogosphere.
    Simple but very precise information… Appreciate your sharing this one.
    A must read article!

Comments have been disabled for this content.