Forums - C# Corner

Forum guidelines
The Gamer

The Gamer

  • 1.7k
  • 9
  • 160

Unable to upload file to store in SQL using C# MVC

Aug 10 2018 9:33 AM
Good morning!
 
I was able to do this in asp.net but I am moving to MVC using C# and am stuck.
 
Here is my view:
  1. @using (Html.BeginForm())    
  2. {    
  3.     @Html.AntiForgeryToken()    
  4.     <input type="file" id="bgFileUpload" name="bgFileUpload" />    
  5.     <input type="submit" id="createNewBtn_id" name="submitNew" value="submitNew" />    
  6. }    
and then I have this in my controller:
 
  1. public ActionResult Admin(string submitNew)  
  2.        {  
  3.            uploadFileToSqlDb();  
  4.            return View();  
  5.        }  
The above calls my function. Here is the function:
  1. [HttpPost]  
  2.        public ActionResult uploadFileToSqlDb()  
  3.        {              
  4.            byte[] uploadedFile = new   
  5.            byte[Request.Files["bgFileUpload"].InputStream.Length];  
  6.   
  7.            return View();  
  8.        }  
 
But everytime I get a "Object reference not set to an instance of an object". Any suggestions for a noobie such as myself?
Everything I have seen online I appear to be doing incorrectly or possibly interpretting it wrong. 
 
 

Answers (2)