Posted By

jink on 10/04/10


Tagged

file upload MVC


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mheinrich


ASP.net MVC: Accept file upload from all browsers.


 / Published in: ASP
 

I use this action code with http://valums.com/ajax-upload/ to handle the upload from any browser.

  1. byte[] uploadedbytes = null;
  2.  
  3. //IE:
  4. if (Request.Files.Count > 0)
  5. {
  6. foreach (string file in Request.Files)
  7. {
  8. HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
  9. if (hpf.ContentLength == 0)
  10. continue;
  11.  
  12. uploadedbytes = new byte[hpf.InputStream.Length];
  13. hpf.InputStream.Read(uploadedbytes, 0, uploadedbytes.Length);
  14.  
  15. break;
  16. }
  17. }
  18.  
  19. //Chrome/FF:
  20. else if (Request.InputStream != null && Request.InputStream.Length != 0)
  21. {
  22. uploadedbytes = new byte[Request.InputStream.Length];
  23. Request.InputStream.Read(uploadedbytes, 0, uploadedbytes.Length);
  24. }
  25.  
  26. if (uploadedbytes != null && uploadedbytes.Length != 0)
  27. {
  28. //handle the upload here.
  29. }

Report this snippet  

You need to login to post a comment.