Usung ASP.NET MongoDB.Driver和我无法保存ContextType

时间:2014-10-18 02:08:48

标签: mongodb mime-types mongodb-.net-driver gridfs asp.net-mvc-5.1

从课程开始:Using MongoDB with ASP.NET MVC 演示:Displaying Rental Images

^如果你有Pluralsight 在本课程中,我们将图像附加到Rental文档,然后我们使用一些剃刀和GetImage Action在AttachImage.cshtml视图上显示图像。我相信所有这些都有效,图像会附加到数据库中的文档中。

问:当我们将图像保存到数据库时,为什么ContentType没有被添加到数据库中的fs.files集合(GridFS)?

注意:我认为控制器内部的代码是罪魁祸首:

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

使用GridFS证明图像已存储
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页面
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);
}

我不知道还有什么可以做的检查,一切不仅从我的角度来看是正确的,而且从课程说明到T恤(据我所知)..

1 个答案:

答案 0 :(得分:3)

将MongoGridFSCreateOptions选项传递给上次调用作为最后一个参数:

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

谢天谢地,这是一个很容易解决的问题:)