从数据库中保存和检索图像 [英] Save and Retrive Image from Database

查看:135
本文介绍了从数据库中保存和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在做一个项目,我必须将图像保存在数据库中,然后在主页上显示/检索它们.我可以将图像保存在文件夹中,但无法检索它们.谁能给我一个适当的解决方案..

我的模型班很喜欢

Hi, I am doing a project where I have to save image in database and then show/retrieve them in main page. I can save image in a folder but doesn''t retrieve them. Can anyone give me a proper solution for it..

My Model Class is likes

public class Product
 {
     [Key]
     public int ProductId { set; get; }

     public IEnumerable<HttpPostedFileBase> Images { get; set; }
 }


我的控制器很喜欢


My Controller is likes

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Product product)
{
    if (ModelState.IsValid)
    {
        db.Entry(product).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

     return View(product);
}


我的观点"很喜欢


My View is likes

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Product</legend>
        <div class="editor-field">
            <h3>Image:</h3>
            <input type="file" name="Image" id="Images" multiple />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

推荐答案

1.第一种解决方案是您应该将SQL数据库中的图像保存到图像"字段中,这样,图像将被保存为字节数组.

2.第二种解决方案是在您的Web应用程序中有一个Doc文件夹用于存储文件,在这种情况下,您必须将该文件夹的访问/权限授予web.config.

您可以在我的下一篇文章中看到实现的两种解决方案(提供了ASP.NET MVC4.0源代码):
1. First solution is that you should save the images in the SQL database into "Image" fields, so in this way the image will be saved as array of bytes.

2.Second solution is to have into your web application a Doc folder used for storing the files, and in this case you must give access/permission to this folder into the web.config.

You can see both solutions implemented into my next article (with ASP.NET MVC4.0 source code provided): MVC Basic Site: Step 3 – Dynamic Layouts and Site Admin with: AJAX, jqGrid, Controller Extensions, HTML Helpers, and more[^]. See details especially in ImageResult class and also in OpenFileResult class and their usage in the application.


您可以将整个路径存储在DB中,其中图像存储在文件夹中.然后检索并分配路径image.source =路径.
you can stored whole path in DB in which the images stored in folder. and then retrived and assign the path image.source= path.


尝试:

如何存储和+将图像读取到MVC中的数据库
Try:

How to store and+read an image to database in MVC


这篇关于从数据库中保存和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆