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


/ Published in: ASP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.