0

I can upload file in MVC3 (C#) by using HttpPostedFileBase but i want to know more about upload file as like...

  • How to get

File Size

  • How to get

File Type

  • How can can i filter file upload by file type

please help me

zxprince
  • 387
  • 1
  • 8
  • 21

1 Answers1

3

File Size

Use file.ContentLength where file is you HttpPostedFileBase variable.

File Type

You could use the extension of the filename, but obviously not 100% reliable. If you want a bullet-proof solution you will have to use heuristics. For example look at the contents of the uploaded file to try to guess its type. For example to test for known image formats you could use the the following technique. I also wrote another post about validating against known image formats using a custom validation attribute.

How can can i filter file upload by file type

HTML5 supports specifying content type:

<input type="file" name="file" accept="image/*" />

If your browser doesn't support HTML5 you will have to use some other technology such as Flash or something. You could use existing plugins such as Uploadify or Plupload which provide this functionality by testing the capabilities of the client browser and falling back progressively.

There simply is absolutely nothing you could do with native HTML and javascript in a say IE7 in order to filter the contents of the file input box.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Can you explain about the heuristics about File Type ? – zxprince Jul 09 '12 at 10:19
  • Depending on the file types you need to handle there are various techniques that you will have to employ. For example for certain image types you could look at the first couple of bytes to recognize the type. I have also updated my answer to point you to another example I wrote about validating against known image formats using a custom validation attribute: http://stackoverflow.com/a/6388927/29407 – Darin Dimitrov Jul 09 '12 at 11:17