Abdullah Nadeem

Abdullah Nadeem

  • NA
  • 19
  • 6.6k

Getting image from FormCollection in Controller (MVC)

Aug 15 2018 4:35 PM
Hello everyone from this community! I am a newbie and facing an issue in my application. Please find me a way out. Thanks in anticipation. 
I am getting following error while passing image from my form (view) to my controller using FormCollection:
CS0029 : Cannot implicitly convert type 'string' to 'byte[]'
 
Here's my db class: 
 
 (line#52 is creating conflict in my controller)

  1. namespace KaamAoTemplate.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     using System.ComponentModel;  
  6.     using System.ComponentModel.DataAnnotations;  
  7.     public partial class User  
  8.     {  
  9.         public int UserID { getset; }  
  10.   
  11.         [Required(ErrorMessage ="This field is required!")]  
  12.         public string FirstName { getset; }  
  13.   
  14.         [Required(ErrorMessage = "This field is required!")]  
  15.         public string LastName { getset; }  
  16.   
  17.         [Required(ErrorMessage = "This field is required!")]  
  18.         public string DateofBirth { getset; }  
  19.   
  20.         [Required(ErrorMessage = "This field is required!")]  
  21.         public string Province { getset; }  
  22.   
  23.         [Required(ErrorMessage = "This field is required!")]  
  24.         public string City { getset; }  
  25.   
  26.         public string PostalCode { getset; }  
  27.   
  28.         [Required(ErrorMessage = "This field is required!")]  
  29.         public string CnicNum { getset; }  
  30.   
  31.         [Required(ErrorMessage = "This field is required!")]  
  32.         public string Contact { getset; }  
  33.   
  34.         [Required(ErrorMessage = "This field is required!")]  
  35.         public string Email { getset; }  
  36.   
  37.         [Required(ErrorMessage = "This field is required!")]  
  38.         [DataType(DataType.Password)]  
  39.         public string Password { getset; }  
  40.   
  41.         [Required(ErrorMessage = "This field is required!")]  
  42.         [DataType(DataType.Password)]  
  43.         public string ConfirmPassword { getset; }  
  44.         [Compare("Password")]  
  45.   
  46.         [Required(ErrorMessage = "This field is required!")]  
  47.         public string Qualification { getset; }  
  48.   
  49.         [Required(ErrorMessage = "This field is required!")]  
  50.         public string SkillSet { getset; }  
  51.   
  52.         public byte[] ProfilePicture { getset; }  
  53.   
  54.         public decimal Wallet { getset; }  
  55.   
  56.         public Nullable<byte> Rating { getset; }  
  57.   
  58.         public int GigCount { getset; }  
  59.     }  
  60. }  
  61. //here is my controller: (issue in line # 16 and 31 )
  1. [HttpPost]  
  2. public ActionResult Signup(FormCollection form)  
  3. {  
  4. string FirstName = form["FirstName"];  
  5. string LastName = form["LastName"];  
  6. string DateofBirth = form["DateofBirth"];  
  7. string Province = form["Province"];  
  8. string City = form["City"];  
  9. string PostalCode = form["PostalCode"];  
  10. string CNIC = form["CNIC"];  
  11. string Contact = form["Contact"];  
  12. string Email = form["Email"];  
  13. string Password = form["Password"];  
  14. string Qualification = form["Qualification"];  
  15. string[] SkillSet = form["JobCat"].Split(',');  
  16. byte[] ProfilePicture = form.Get("profilepic");  
  17.   
  18. User user = new User();  
  19. user.FirstName = FirstName;  
  20. user.LastName = LastName;  
  21. user.DateofBirth = DateofBirth;  
  22. user.Province = Province;  
  23. user.City = City;  
  24. user.PostalCode = PostalCode;  
  25. user.CnicNum = CNIC;  
  26. user.Contact = Contact;  
  27. user.Email = Email;  
  28. user.Password = Password;  
  29. user.Qualification = Qualification;  
  30. user.SkillSet = SkillSet.ToString();  
  31. user.ProfilePicture = ProfilePicture;  
  32. KaamAoDBEntities db = new KaamAoDBEntities();  
  33. db.Users.Add(user);  
  34. db.SaveChanges();  
  35.   
  36. return View(user);  
  37. }  
and here's what i have in my view:
  1. for="fileupload">Upload Profile Picture  
  2.                         "file" name="profilepic" value="" id="fileupload">  
  3.                         
      
 

Answers (2)