0

From the course: Using MongoDB with ASP.NET MVC Demo: Displaying Rental Images

^In case you have Pluralsight At this point in the course we had attached the images to a Rental document, next we use some razor and a GetImage Action to display an image on a AttachImage.cshtml view. I believe all of that works, the image is getting attached to the document in the database.

Q: When we save the image to the database, why is the ContentType not getting added to the fs.files collection (GridFS) in the database?

NOTE: I believe the code inside the controller that is culprit is at or around:

//------------------------------------------
var options = new MongoGridFSCreateOptions
{
    Id = imageId,
    ContentType = file.ContentType
};
//------------------------------------------

Proof the image got stored using GridFS
Proof the image got stored using GridFS

Proof ContentType Didn't get saved
Proof ContentType Didn't get saved

AttachImage.cshtml

@model RealEstate.Rentals.Rental
@{ ViewBag.Title = "AttachImage"; }

<h4>Attach Rental Image</h4>

@using (Html.BeginForm(null, null, FormMethod.Post, new {enctype = 
        "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
<div class="form-horizontal">
    <div class="form-group">
        <label class="control-label col-md-2">Decription</label>
        <div class="col-md-10">
            @Model.Description
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Image", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="file" id="file" />
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Attach" class="btn btn-default" />
        </div>
    </div>
</div>
}
@if (Model.HasImage())
//Show Image
{
    <img src="@Url.Action("GetImage", new { id = Model.ImageId })" />
}

AttachImage Page in Browser
AttachImage Page in Browser

RentalsController.cs

namespace RealEstate.Controllers
{
  public class RentalsController : Controller
  {
  public readonly RealEstateContext Context = new RealEstateContext();

  public ActionResult Post()
  {
        return View();
  }

[HttpPost]
public ActionResult Post(PostRental postRental)
{

    var rental = new Rental(postRental);
    Context.Rentals.Insert(rental);
    return RedirectToAction("Index");
}

public ActionResult AttachImage(string id)
{
    var rental = GetRental(id);
    return View(rental);
}

[HttpPost]
public ActionResult AttachImage(string id, HttpPostedFileBase file)
{
    var rental = GetRental(id);
    if (rental.HasImage())
    {
        DeleteImage(rental);
    }

    StoreImage(file, rental);
    return RedirectToAction("Index");
}

public ActionResult GetImage(string id)
{
    var image = Context.Database.GridFS
        .FindOneById(new ObjectId(id));
    if (image == null)
    {
        return HttpNotFound();
    }

    return File(image.OpenRead(), image.ContentType);
}


private Rental GetRental(string id)
{
    var rental = Context.Rentals.FindOneById(new ObjectId(id));
    return rental;
}

private void DeleteImage(Rental rental)
{
    //Access GrdFS & Delete By ID / Pass ImageID converted to ObjectId
    Context.Database.GridFS.DeleteById(new ObjectId(rental.ImageId));
    rental.ImageId = null;
    Context.Rentals.Save(rental);
}

private void StoreImage(HttpPostedFileBase file, Rental rental)
{
    var imageId = ObjectId.GenerateNewId();
    rental.ImageId = imageId.ToString();
    Context.Rentals.Save(rental);
    var options = new MongoGridFSCreateOptions
    {
        Id = imageId,
        ContentType = file.ContentType
    };

        Context.Database.GridFS.Upload(file.InputStream, file.FileName);
}

I don't know what else to do check, everything not only looks right from my perspective, but it's to the tee (As far as I can tell) from the Course Instruction..

Eric Bishard
  • 5,201
  • 7
  • 51
  • 75
  • So it was the last line of code in the Controller. I failed to pass the third parameter which sets the MIME ContentType from the uploaded file. See correct answer from the Pluralsight Author below, ... Thanks Wes! – Eric Bishard Oct 19 '14 at 04:43

1 Answers1

3

pass the MongoGridFSCreateOptions options to the call to Upload as the last argument:

Context.Database.GridFS.Upload(file.InputStream, file.FileName, options);

Thankfully that's an easy enough fix :)

Wes
  • 711
  • 7
  • 6
  • I knew it. Something silly. So in the portion of the course where we add this, you are perfectly clear on what to do. But I must of missed it and the actual code is only on the screen for 4 seconds where it shows the last parameter and then it goes back to a view where you cannot see the last parameter. Again this is completely my fault. But I see why I missed it and I appreciate your help getting it to work. I also should have seen that I was creating the options variable and not using it. That was a huge red flag too. I'm learning though and I love this course. One of my MVC favorites! – Eric Bishard Oct 19 '14 at 04:40
  • 1
    Glad you are enjoying the course, yeah it was hard to work with 1024x768 and the 16 point font that's requested we use! But that's changing for future courses. Do you have access to the premium materials, if so there's a finished example in there. – Wes Oct 20 '14 at 05:42
  • I don't have the source code I skimped on my subscription. I actually like not having the source code because it means I have to force myself to write every line. As you can see I almost pulled it off without any help at all. lol I do encourage others to take this course though, it's not a complete replacement for our RDBMS projects, but it will show you where you can use a relational DB for many projects for reasons of Horiz scale or ease of development. – Eric Bishard Oct 20 '14 at 07:26
  • The harder the learning, the better. There's science behind that actually :) – Wes Oct 21 '14 at 14:34