Reputation: 5341
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 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
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..
Upvotes: 0
Views: 404
Reputation: 731
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 :)
Upvotes: 3