title: ASP MVC 2 Uploading file to database (blob)
body: <p>I am trying to upload a file via a form and then save in in SQL as a blob.</p>
  
  <p>I already have my form working fine, my database is fully able to take the blob and I have a controller that take the file, saves it in a local directory:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
          public ActionResult FileUpload(int id, HttpPostedFileBase uploadFile)
          {
  
              //allowed types
              string typesNonFormatted = "text/plain,application/msword,application/pdf,image/jpeg,image/png,image/gif";
              string[] types = typesNonFormatted.Split(',');
  
              //
              //Starting security check
  
              //checking file size
              if (uploadFile.ContentLength == 0 &amp;&amp; uploadFile.ContentLength &gt; 10000000)
                  ViewData["StatusMsg"] = "Could not upload: File too big (max size 10mb) or error while transfering the file.";
  
              //checking file type
              else if(types.Contains(uploadFile.ContentType) == false)
                  ViewData["StatusMsg"] = "Could not upload: Illigal file type!&lt;br/&gt; Allowed types: images, Ms Word documents, PDF, plain text files.";
  
              //Passed all security checks
              else
              {
                  string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                                  Path.GetFileName(uploadFile.FileName)); //generating path
                  uploadFile.SaveAs(filePath); //saving file to final destination
  
                  ViewData["StatusMsg"] = "Uploaded: " + uploadFile.FileName + " (" + Convert.ToDecimal(uploadFile.ContentLength) / 1000 + " kb)";
  
                  //saving file to database
                  //
                  //MISSING
              }
  
              return View("FileUpload", null);
          }
  </code></pre>
  
  <p>Now all I am missing is putting the file in the database. I could not find anything on the subject... I found some way to do it in a regular website but nothing in MVC2.</p>
  
  <p>Any kind of help would be welcome!</p>
  
  <p>Thank you.</p>