Saving Image To Database Using MVC

Saving an image to a database is not hard to do as you might think. I developed a sample MVC application demonstrating this. You may get the complete code from my github.

Below is the Model I have. The important thing here is byte[] which is the one that is saved in the database while the HttpPostedFileBase is the data posted from the View.

public class Car
      , Key()]
  public int CarId { get; set; }

  public string Name { get; set; }

  public string Type { get; set; }

  public byte[] ImageFile { get; set; }

  public string ImageContentType { get; set; }

  public string ImageFileName { get; set; }

  List<Maintenance> MaintenanceSchedule { get; set; }

  public HttpPostedFileBase FileAttach { get; set; }


This is the View control code in its simple form. You can see the complete code of the control from this link.

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = 
       "multipart/form-data" }))
    <div class="form-group">
        @Html.LabelFor(model => model.ImageFile, htmlAttributes: 
          new { @class = "control-label col-md-2" })
            <div class="col-md-10">
        @Html.TextBoxFor(m => m.FileAttach, new { type = "file", 
           accept = "image/jpeg,image/jpg,image/png", 
           placeholder = Html.DisplayNameFor(m => m.FileAttach), 
           @class = "form-control" })
        @Html.ValidationMessageFor(model => model.FileAttach, "", 
           new {@class = "text-danger" })

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
           <input type="submit" value="Create" 
                 class="btn btn-default" />

Here is the code in the Controller to save in the database using Entity Framework.

public ActionResult Create([Bind(Include = 
      "CarId,Name,Type,ImageFile,FileAttach")] Car car)
    if (ModelState.IsValid)
        return RedirectToAction("Index");
    return View(car);

private void UpdateFileDetails(Car car)
    HttpPostedFileBase postedFile = car.FileAttach;
    if (postedFile != null)
         byte[] bytes;
         using (BinaryReader br = new BinaryReader
                bytes = br.ReadBytes(postedFile.ContentLength);

         car.ImageFileName = Path.GetFileName(postedFile.FileName);
         car.ImageContentType = postedFile.ContentType;
         car.ImageFile = bytes;

As you can see from the code above, with a few lines of code, you will be able to save an image to a database. Hope this helps anyone who is looking to implement this in their project!

Encrypting PDF with a Password

If you are developer and looking for a way to put a password on a PDF file for free, you may want to look into qpdf command line tool. You can install this in Linux and Windows as well.

I develop using c# and was able to utilize this tool inside the program that I was doing. You can actually use this in any kind of programming language by just calling it through a command line interface.

Please see code snippet below.

public static string qpdfEncrypt(string inFile, string outFile, 
     string password)
     using (System.Diagnostics.Process pProcess = new 
        pProcess.StartInfo.FileName = 
           + @"\\qpdf\\qpdf.exe";
        pProcess.StartInfo.Arguments = $"--encrypt {password} 
           {password} 256 -- \"{inFile}\" \"{outFile}\"";
        pProcess.StartInfo.UseShellExecute = false;
        pProcess.StartInfo.RedirectStandardOutput = true;
        pProcess.StartInfo.RedirectStandardError = true;
        pProcess.StartInfo.WindowStyle = 
        pProcess.StartInfo.CreateNoWindow = true;
        string output = pProcess.StandardError.ReadToEnd();
        return output;

As you can see, the code just opens the process command interface and calls the qpdf executable. You can actually use the same code in calling any kind of command line utility that you want to integrate in your code.

Happy Coding!

Online Bayanihan

“Bayanihan is a traditional system of mutual assistance in which the members of a community work together to accomplish a difficult task. It is also a spirit of civic unity and cooperation among Filipinos”.    –Oxford Dictionary

Each member helps one another for a common goal where each member cannot do on their own. This Filipino trait has existed for a long time in Philippine culture. We see images of Filipinos helping one another move a house from one place to another.  In times of calamities, we see Filipinos volunteering or donating with whatever they can to those affected. For those abroad, Filipinos are happy to see and help newly-arrived Filipinos get accustomed to a foreign land.  No matter where the Filipinos may be, we can always see the spirit of bayanihan alive.

Continue reading

Filipino IT: The New Ilustrados in the Online World

Ilustrados (the learned or enlightened ones) constituted the Filipino educated class during the Spanish colonial period in the late 19th century. They were the middle class who were educated in Spanish and exposed to Spanish liberal and European nationalist ideals. Some famous ilustrados were Jose Rizal, Graciano López Jaena, Marcelo H. del Pilar, Mariano Ponce, Antonio Luna and José Rizal. –Wikipedia

Continue reading

Is having a passive online business important?

If you want to work from home and spend time with your family, a passive online business may be the best way for you.

Before anything else, let us define an online business. An online business is a business done through the internet. It has become more popular since the late 90’s given that we are currently in the information age.

The internet has lowered the barrier to entry to create an online business. We don’t need to have a big capital to open an online business nor do we need a big space. If you have a unique product or idea, you can find a way to sell it online.

Here are some examples of online businesses and how they make money:

Continue reading