ASP.NET MVC3:上传图像并将其保存到数据库

时间:2011-08-31 16:58:53

标签: asp.net-mvc-3 file-upload

我正在研究Steven Sanderson的书(Pro ASP.NET MVC 3)。我在。我已经复制了每个单词中的单词,但它不起作用。

这是行动方法

public ActionResult Edit(Product product, HttpPostedFileBase image)
{
  if(ModelState.IsValid)
  {
    if(image != null)
    {
      product.ImageMimeType = image.ContentType;
      product.ImageData = new byte[image.ContentLength];
      image.InputStream.Read(product.ImageData, 0, image.ContentLength); 
    }

    //...Save product in the database using Entity Framework
  }
}

这是如何在剃须刀页面上显示图像

<img width="150" height="150"
  src = "@Url.Action("GetImage", "Home", new { Model.ProductID })" /> 

最后,GetImage

public FileContentResult GetImage(int productID)
    {
        Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productID);
        if (prod != null)
        {
            return File(prod.ImageData, prod.ImageMimeType);
        }
        else
        {
            return null;
        }
    }

修改

我从开始到结束都遵循了整个过程(在调试过程中),这就是我能说的:

  • 按下视图上的“保存”按钮后, HttpPostedFileBase 对象不为空。

  • 调用db.SaveChanges()方法后,在数据库表中添加了一行。

  • 当我调用GetImage时,它不会返回null。

  • 但在视图中,没有显示图像

    感谢您的帮助

8 个答案:

答案 0 :(得分:2)

File应为FileContentResult,因为它是字节而不是磁盘上的实际文件。 img应为prod,对吗?

public FileContentResult GetImage(int productID)
{
    Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productID);
    if (prod != null)
    {
        return new FileContentResult(prod.ImageData, prod.ImageMimeType);
    }
    else
    {
        return null;
    }
}

答案 1 :(得分:1)

像这样修改你的行动方法:

<img width="150" height="150" src = "@Url.Action("GetImage", "Home", new { @id = Model.ProductID })" /> 

除了那个小差异(添加id参数)之外,你的代码与我的代码非常相似,我的图片加载得很好。顺便说一句,如果您查看页面的HTML源代码,您会看到:

/GetImage/<some number here>

/GetImage/

作为src属性的值?如果是后者,这绝对应该解决问题。如果是前者,您可能需要启用GET次请求。

答案 2 :(得分:0)

以下是让Steven Sanderson示例工作的答案,改变你的保存程序:

            if (image != null)
            {

                var product = new Product();

                product.FileName = image.FileName; //<- optional filename
                product.ImageMimeType = image.ContentType;
                int length = image.ContentLength;
                byte[] buffer = new byte[length];
                image.InputStream.Read(buffer, 0, length);
                product.ImageData = buffer;

              //Save product to database 

            }

答案 3 :(得分:0)

我也有同样的问题。 只需将控制器中Edit方法的一部分更改为这样:

 if (image != null)
            {
                mvcImages img = db.mvcImages.Where(p => p.p_id == prod.p_id).FirstOrDefault();
                prod.p_imageMimeType = image.ContentType;
                byte[] buffer = new byte[image.ContentLength];
                image.InputStream.Read(buffer, 0, image.ContentLength);
                prod.p_imageData = buffer;
                img.p_imageMimeType = prod.p_imageMimeType;
                img.p_imageData = prod.p_imageData;
                db.SaveChanges();
            }

工作正常。 还要记住在与“if”命令相同的括号内保存更改。

答案 4 :(得分:0)

更简单的解决方法是更改​​产品类中的以下行:

从:改变:

public byte ImageData {get;set;}

要:

public byte[] ImageData{get;set;}

答案 5 :(得分:0)

我遇到同样的问题..试试这个:

public ActionResult Edit(Product product, HttpPostedFileBase image)
{
  if(ModelState.IsValid)
  {
     if(image != null)
     {
       product.ImageMimeType = image.ContentType;
       product.ImageData = new byte[image.ContentLength];

       iname.InputStream.Position = 0; // This is what makes the difference

       image.InputStream.Read(product.ImageData, 0, image.ContentLength); 
     }

//...Save product in the database using Entity Framework

}

答案 6 :(得分:0)

问题在于Action方法.. 它应该是 你必须更改控制器的名称...“GetImage”功能在“管理”控制器..其余的代码是好的...

答案 7 :(得分:0)

简单打开entity文件夹中的Products.cs文件并更改

public byte ImageData {get;set;}

要:

public byte[] ImageData{get;set;}
相关问题